[Tkinter-discuss] Unexpected Checkbutton behaviour

Fredrik Lundh fredrik at pythonware.com
Wed Nov 29 08:03:38 CET 2006


Gerardo Juarez wrote:


> 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

you must use a Tkinter variable object, not an arbitrary Python variable:

http://effbot.org/tkinterbook/checkbutton.htm#patterns

(note that in Python, the parameter expressions are *always* evaluated 
before the function is actually called, so "variable=x" will pass a zero 
to the checkbutton.  you cannot pass references to variable names in 
Python, only references to objects).

</F>



More information about the Tkinter-discuss mailing list