[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.python

Re: Tkinter variable trace problem

Fredrik Lundh

1/7/2008 10:56:00 PM

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>