[Tkinter-discuss] Unexpected Checkbutton behaviour

Cam camfarnell at cogeco.ca
Wed Nov 29 05:09:37 CET 2006


Hi Gerardo,

You can't user a regular python variable for "x", you need to create, in 
this case, an IntVar "control variable". See revised code below, also 
see  http://infohost.nmt.edu/tcc/help/pubs/tkinter/control-variables.html

Cam Farnell


from Tkinter import *

def display():
     print x.get()

root = Tk()
x = IntVar()
x.set(0)
check = Checkbutton(root, variable=x)
check.pack()
button = Button(text='Display variable', command=display)
button.pack()
root.mainloop()


Gerardo Juarez wrote:
> Hi, 
> 
> I'm having problems with a widget, which  I've been able to isolate to the 
> following example:
> 
> from Tkinter import *
> 
> def display():
>     global x
>     print x
> 
> root = Tk()
> x = 0
> check = Checkbutton(root, variable=x)
> check.pack()
> button = Button(text='Display variable', command=display)
> button.pack()
> root.mainloop()
> 
> As far as I understand the reference, when I click the button, the
> variable 'x' should reflect the state of the check button, and viceversa,
> if I change the value of x, from 0 to 1, the widget reflects the change.
> Well, in practice, I get a constant zero, whether I click or not the
> checkbutton. I must be missing something, but I can;t figure out what it
> is. Any comments will be welcome.
> 
> TIA,
> Gerardo
> 
> 
> _______________________________________________
> Tkinter-discuss mailing list
> Tkinter-discuss at python.org
> http://mail.python.org/mailman/listinfo/tkinter-discuss
> 


More information about the Tkinter-discuss mailing list