[Tutor] Checkbox problem in Tkinter

Alan Gauld alan.gauld at btinternet.com
Mon Jan 10 10:30:03 CET 2011


"ANKUR AGGARWAL" <coolankur2006 at gmail.com> wrote

> from Tkinter import *
> root=Tk()
> var=StringVar()
> c=Checkbutton(root,text="hello",variable=var,onvalue="hhhh",offvalue="gggg")
> c.pack()
> root.mainloop()

FWIW I get a different problem, namely that the var does not seem
to get updated nor does changling the var seem to alter the buuttons
state. But at least the button displays OK with 'hello' in the label 
and
I can select/deselect it with the mouse. its just the change to the 
variable that is broken.
If I write an explicit event handler:

def f():
      var = 'hhhh' if var == 'gggg' else 'gggg'

Then var changes OK. Here is my code(from Pythonwin):

>>> def f(): global var; var = 'on' if var == 'off' else 'off';print 
>>> var
...
>>> top = Tk()
>>> var = StringVar()
>>> c = Checkbutton(top,text='txt', variable=var, onvalue='on', 
>>> offvalue='off', command=f)
>>> c.pack()
>>> top.mainloop()
off
on
off
on
off
on

HTH,


-- 
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/




More information about the Tutor mailing list