Hello all,
I’m learning tkinter
which has been… interesting. I’m going off of the TkDocs tutorial because the tkinter
documentation is intentionally sparse. There is one thing that I can’t seem to find a satisfying answer to in either though.
ttk.Entry
widgets support the validatecommand
widget option (a keyword argument to the ttk.Entry
class constructor), who’s value should be a callable to be invoked in the event that the content of the widget is changed by the user, etc.
When working in TCL itself, and within such a callback, percent substitutions may be used which will be expanded to various values, such as the new value that the entry widget will contain in the event that the validation is successful, a string indicating the condition that triggered the validation, etc.
for reasons I don’t fully understand, in order to use said percent substitutions in the python validation callback, the callback must be ‘registered’ and the specific percent substitutions you wish to be passed to it as arguments named explicitly. This is done with the .register() method of a widget object like so…
def inputValidation(a, b)
# some validation code
input_validation_wrapper = (root.register(inputValidation), '%P', '%V')
input_widget = ttk.Entry(root, validate='all', validatecommand=input_validation_wrapper)
I don’t understand the syntax of the third line, it appears to me that the right side of the assignment operator is just a triple tuple, which will be stored in the variable. However all the material I’ve seen says that the validatecommand
option to the ttk.Entry
widget only accepts a reference to a callable, which in the above example is not the case.
I also can’t work out which widget object the .register()
method should be called on, in the example I’ve seen it’s just called on the object that symbolizes the root window but TkDocs also says it can be called on other widgets, without elaborating.
relevant section of the TkDocs tutorial can be found here, scroll down a little to the section headed “validation”
Any help would be much appreciated! I find it strange how the tutorial just glosses over this pretty arcane aspect of tkinter
.