Jump to content

python/tkinter trace_variable() oddness...


Dak

Recommended Posts

anyone got any idea why this doesn't work?

 

also: thanks again to Aeternus for helping on irc.

 

basically, even tho i'm trying to set the trace on the last Entry box's textvar, it insists on setting it on the first one i created...

 

from Tkinter import *

#what I want is a variable number of Entry boxes for the user; 
# so, when the user starts to fill in the last box, I want to 
# generate another one for him 

class ThingyForTheMiddle():
def __init__(self,parent):
	self.box = Frame(parent)
	self.x = StringVar()
	self.xEnt = Entry(self.box,textvar=self.x,width = 10)
	self.box.pack(),self.xEnt.pack()

class Preferences():
def __init__(self):
	self.prefs = Toplevel()

	self.middleBox = Frame(self.prefs).pack()
	self.middleBit = [] 	#list of frames in the middleBox frame
	self.newBit()		#makes a new Entry
	#trace Entry so we know when to make a new one
	self.activeTrace = self.middleBit[-1].x.trace('w',self.watchme) 

def newBit(self):
	self.middleBit.append(ThingyForTheMiddle(self.middleBox)) 

def watchme(self,x,y,z):
	#delete the old trace
	self.middleBit[-1].x.trace_vdelete('w',self.activeTrace)
	#make a new bit for the middle
#		print '1: ',self.activeTrace
	self.newBit()
#		print '2: ',self.activeTrace
	#try to set the trace up on the new Entry box...
	self.activeTrace = self.middleBit[-1].x.trace('w',self.watchme)
	#commenting-out the above line shows the trace is 
	# being correctly deleted... but then it's re-instated to
	# exactly the same box (even tho ~~middleBit[-1] is now different).
#		print '3: ',self.activeTrace

root=Tk()
myPref = Preferences()
root.mainloop()

 

any clues would be appreciated

 

Don't worry, finally figured out how to work around it:

 

	def newBit(self):
	while 1:
		self.middleBit.append(ThingyForTheMiddle(self.prefs))
		self.prefs.wait_variable(self.middleBit[-1].x)

 

OTOH, i'm still curious as to why the original way didn't work... anyone recon it might be a bug I should report?

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.