471,310 Members | 1,381 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,310 software developers and data experts.

Tkinter variable trace problem

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

It produces the following output. The first three lines appear right away, and the exception occurs when you type in the entry widget:
>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
Jan 7 '08 #1
1 8803
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

var.trace('w', cb)

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

tk.mainloop()
##end code

It produces the following output. The first three lines appear right away, and the exception occurs when you type in the entry widget:
>>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
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
Jan 8 '08 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Russell E. Owen | last post: by
6 posts views Thread by max(01)* | last post: by
5 posts views Thread by max(01)* | last post: by
reply views Thread by Brian Kazian | last post: by
3 posts views Thread by William Gill | last post: by
4 posts views Thread by Ron Provost | last post: by
1 post views Thread by Michael Yanowitz | last post: by
3 posts views Thread by H J van Rooyen | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.