Discussion:
Tkinter variable trace problem
Peter Otten
2008-01-08 08:21:32 UTC
Permalink
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
print "callback called with name=%r, index=%r, mode=%r" % (name, index, mode)
varValue = tk.getvar(name)
print " and variable value = %r" % varValue
var.trace('w', cb)
var.set('test')
entry = Tkinter.Entry(tk, textvariable=var)
entry.pack()
tk.mainloop()
##end code
test.py
PY_VAR0
callback called with name='PY_VAR0', index='', mode='w'
and variable value = 'test'
callback called with name='PY_VAR0', index='', mode='w'
Exception in Tkinter callback
File "C:\Python25\lib\lib-tk\Tkinter.py", line 1403, in __call__
return self.func(*args)
File "D:\APCC\Projects\Utilities\VisualData\test.py", line 9, in cb
varValue = tk.getvar(name)
File "C:\Python25\lib\lib-tk\Tkinter.py", line 421, in getvar
return self.tk.getvar(name)
TclError: can't read "PY_VAR0": no such variable
A quick look in the Tkinter source reveals that you need
tk.globalgetvar(), not tk.getvar().

I would actually recommend var.get(), so that you don't have to mess with
these internals at all.

Peter
C Martin
2008-01-08 17:22:45 UTC
Permalink
Thank you for your replies. I understand there are better ways to handle 'normal' UI behavior, but I was curious about the trace feature and wanted to understand how it worked. I may not actually need it in my project, but I wanted to see what my options were.

Thanks again.


--------------------------------------
Protect yourself from spam,
use http://sneakemail.com
C Martin
2008-01-07 22:13:41 UTC
Permalink
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

var.trace('w', cb)

var.set('test')
entry = Tkinter.Entry(tk, textvariable=var)
entry.pack()

tk.mainloop()
##end code
test.py
PY_VAR0
callback called with name='PY_VAR0', index='', mode='w'
and variable value = 'test'
callback called with name='PY_VAR0', index='', mode='w'
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Python25\lib\lib-tk\Tkinter.py", line 1403, in __call__
return self.func(*args)
File "D:\APCC\Projects\Utilities\VisualData\test.py", line 9, in cb
varValue = tk.getvar(name)
File "C:\Python25\lib\lib-tk\Tkinter.py", line 421, in getvar
return self.tk.getvar(name)
TclError: can't read "PY_VAR0": no such variable
--------------------------------------
Protect yourself from spam,
use http://sneakemail.com
Fredrik Lundh
2008-01-07 22:55:31 UTC
Permalink
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
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>

Loading...