Tkinter variable trace problem

Fredrik Lundh fredrik at pythonware.com
Mon Jan 7 17:55:31 EST 2008


C Martin wrote:

> What am I doing wrong in this code? The callback doesn't work from the Entry widget.
> 
> ##start code
> import Tkinter
>   
> tk = Tkinter.Tk()
> var = Tkinter.StringVar()
> print var._name
> 
> def cb(name, index, mode):
>   print "callback called with name=%r, index=%r, mode=%r" % (name, index, mode)
>   varValue = tk.getvar(name)
>   print "    and variable value = %r" % varValue

iirc, getvar only looks at local Tcl variables when used inside a 
callback.  variables created with StringVar are global Tcl variables.

but instead of monkeying around at the Tcl level, why not just solve 
this on the Python level instead?

     def cb(variable, name, index, mode):
         ...

     var.trace('w', lambda *args: cb(var, *args)) # or some variation 
thereof

(and for this specific case, Entry content tracking, binding to 
KeyRelease and possibly also ButtonRelease is usually good enough).

</F>




More information about the Python-list mailing list