Using bound variables in Tkinter events

Peter Otten __peter__ at web.de
Thu Apr 1 02:08:52 EST 2004


Derek Fountain wrote:

>> import Tkinter as tk
>> root = tk.Tk()
>> 
>> def check(action):
>>     print {"1":"inserting", "0": "deleting"}.get(action, "should never
>> happen")
>>     return True
>> checkId = root.register(check)
>> 
>> entry = tk.Entry(root, validate="key", validatecommand=checkId + " %d")
>> entry.pack()
>> root.mainloop()
>> 
>> The above was found by trial and error, so no warranties :-)
> 
> Awesome, thank you. I don't understand what's going on though. Can you
> explain a bit? I don't understand the mechanism which is expanding the %d.
> Is there some magic in the tk.Entry constructor which does it, or is that
> root.register() doing something clever?

register() wraps a python function into a tcl function and returns the new
tcl function's name (can they start with a number?). If you provide a
string instead of a python function reference as a callback for (e. g.)
validatecommand, it is passed unaltered to tcl. 
That's all there is to it - at least so my theory goes.

Peter




More information about the Python-list mailing list