Using bound variables in Tkinter events

Derek Fountain devnull at example.com
Wed Mar 31 21:21:28 EST 2004


> 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?


> You could use check() directly, like
> 
> entry = tk.Entry(root, validate="key", validatecommand=check)
> 
> but then it will be called without parameters.

That's as far as I'd got and I was beginning to think it was impossible to
get the parameters. At least I now know it can be done, even if I don't
understand how!



More information about the Python-list mailing list