Tkinter.Radiobutton question
Svein Brekke
svbrk at start.no
Mon Jan 7 07:06:01 EST 2002
When reading the state variable in the '<ButtonRelease-1>' callback
from a Radiobutton widget, I always get the old variable value, not
the new one.
Try running the following code:
#Start code
from Tkinter import *
import thread,time
class MyRadiobutton(Radiobutton):
def __init__(self,w,text,variable=None,value=None):
Radiobutton.__init__(self,w,text=text,variable=variable,value=value)
self.variable = variable
self.bind('<ButtonRelease-1>',self.onclick)
self.pack()
def onclick(self,e):
print 'onclick called, variable =',self.variable.get()
root = Tk()
v = StringVar()
v.set('r')
r = MyRadiobutton(root,'R',variable=v,value='r')
s = MyRadiobutton(root,'S',variable=v,value='s')
root.mainloop()
#End of code
This brings up a window with two radiobuttons, named R and S, where R
is selected. Clicking on the S button, prints the following message:
onclick called, variable = r
I.e. the value picked out in the callback is the old one.
Is there way to read the new value from inside the callback, or do I
have to spawn a new thread for reading the new value after a given
delay (it works, but forces me to add syncronization overhead in my
program)?
regards
Svein Brekke
More information about the Python-list
mailing list