473,597 Members | 2,413 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.StringV ar()
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(t k, textvariable=va r)
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\li b\lib-tk\Tkinter.py", line 1403, in __call__
return self.func(*args )
File "D:\APCC\Projec ts\Utilities\Vi sualData\test.p y", line 9, in cb
varValue = tk.getvar(name)
File "C:\Python25\li b\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 9483
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.StringV ar()
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(t k, textvariable=va r)
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\li b\lib-tk\Tkinter.py", line 1403, in __call__
return self.func(*args )
File "D:\APCC\Projec ts\Utilities\Vi sualData\test.p y", line 9, in cb
varValue = tk.getvar(name)
File "C:\Python25\li b\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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
4700
by: Marc | last post by:
Hi all, I was using Tkinter.IntVar() to store values from a large list of parts that I pulled from a list. This is the code to initialize the instances: def initVariables(self): self.e = IntVar() for part, list in info.masterList.items():
2
3863
by: Russell E. Owen | last post by:
I want to support execution of simple user-written scripts in a Tkinter application. The scripts should be able to wait for data and such without hanging the GUI (and without having to write the script as a bunch of asynchronously called subroutines). I decided to use Tkinter's wait_variable. I built a "script runner" object that has suitable wait methods. Internally each of these methods registers a callback that sets a variable when...
6
17975
by: max(01)* | last post by:
hi people. when i create a widget, such as a toplevel window, and then i destroy it, how can i test that it has been destroyed? the problem is that even after it has been destroyed, the instance still exists and has a tkinter name, so testing for None is not feasible: >>> import Tkinter >>> fin = None >>> fin1 = Tkinter.Toplevel()
5
2045
by: max(01)* | last post by:
hello. the following code: 1 from Tkinter import * 2 3 class MiaApp: 4 def __init__(self, genitore): 5 self.mioGenitore = genitore 6 self.i = IntVar()
0
1287
by: Brian Kazian | last post by:
I am currently having trouble running a piece of code I have written. My main code is called new.py, and when I run it, it pops up the TKinter root window as specified. However, if I try perform a function that uses an imported module, the code crashes, giving me this error: Exception in Tkinter callback Traceback (most recent call last): File "C:\Python24\lib\lib-tk\Tkinter.py", line 1345, in __call__ return self.func(*args)
3
2223
by: William Gill | last post by:
Working with tkinter, I have a createWidgets() method in a class. Within createWidgets() I create several StringVars() and assign them to the textvariable option of several widgets. Effectively my code structure is: def createWidgets(self): ... var = StringVar() Entry(master,textvariable=var) ...
4
2680
by: Ron Provost | last post by:
Hello, I'm using python 2.4.2 on Win XP Pro. I'm trying to understand a behavior I'm seeing in some Tkinter code I have. I've reduced my question to a small piece of code: #####BEGIN CODE ################# import Tkinter as Tk
1
3589
by: Michael Yanowitz | last post by:
Hello: Below I have included a stripped down version of the GUI I am working on. It contains 2 dialog boxes - one main and one settings. It has the following problems, probably all related, that I am hoping someone knows what I am doing wrong: 1) Pressing the Settings.. Button multiple times, brings up many instances of the Settings Panel. I just want it to bring up one. Is there an easy way to do that?
3
1996
by: H J van Rooyen | last post by:
Hi, Still struggling with my GUI exercise - I have the following lines of code in a routine that is bound at <Key-Returnto an instance of Entry : self.disp.Amount_des = Label(self.disp, text = self.dis_string, fg = 'black', bg = 'yellow') self.disp.Amount_des.grid(row = self.rownum, column=0, sticky = 'nesw')
0
7959
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
7883
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8263
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8254
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6677
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
5842
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5421
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
3917
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1226
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.