Using bound variables in Tkinter events

Peter Otten __peter__ at
Wed Mar 31 17:09:39 CEST 2004

Derek Fountain wrote:

> I'm coming to Tkinter from Tcl/Tk. In Tcl I can get a variable in an event
> using the %<X> substitution mechanism. For example, I can set up a command
> like:
>  entry .e -validate 1 -vcmd "checkkey %d"
> knowing that the '%d' will be replaced by something useful - whether the
> entry widget has recieved an insert or deletion in this case. The checkkey
> procedure will recieve "insert", "delete" or whatever as its first
> parameter.
> What is the Tkinter way of getting that %d value?

import Tkinter as tk
root = tk.Tk()

def check(action):
    print {"1":"inserting", "0": "deleting"}.get(action, "should never
    return True
checkId = root.register(check)

entry = tk.Entry(root, validate="key", validatecommand=checkId + " %d")

The above was found by trial and error, so no warranties :-)
You could use check() directly, like

entry = tk.Entry(root, validate="key", validatecommand=check)

but then it will be called without parameters.


More information about the Python-list mailing list