[Tutor] Checkbox problem in Tkinter

Alan Gauld alan.gauld at btinternet.com
Mon Jan 10 19:21:50 CET 2011


"Peter Otten" <__peter__ at web.de> wrote


>> I can select/deselect it with the mouse. its just the change to the
>> variable that is broken.

This was the problem using the Stringvar.


>> 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
>
> I think that is confusing. Tkinter is not supposed to rebind the 
> global name
> 'var', it invokes its set() method. Try

Yes, my bad, I should have removed the StringVar assigment it was
just a carryover from the previous attempt. The problem was that
the StringVar was never changing when the control was clicked.
This was my attempt to create a manual solution that avoided the
automated StringVar approach.

> def command():
>    print var.get()

This raises an interesting point though. I was printing var - which
never changed - I forgot about the need to go through a get()
method. Time for another experiment....

Alan G. 




More information about the Tutor mailing list