Tkinter Checkbutton initialization problem

P

Paul Rubin

Python 2.4, Windows XP. If I say:

f = Frame()
f.grid()
v = IntVar()
c = Checkbutton(f, text='hi there', variable=v)
c.grid()
f.mainloop()

then the checkbutton should initially display as being checked.
But it doesn't. It shows up unchecked, even though v.get() returns 1.
The select operation DOES update the display if there's no variable.

It works properly under Linux with (I think) Python 2.3. I don't have
a 2.4.1 Windows installation to try right now.

Is this a known bug? Is it fixed in 2.4.1? Is there a simple
workaround, that lets me initialize a checkbutton and still be able to
read its state?

Thanks.
 
S

Scott David Daniels

Paul said:
Python 2.4, Windows XP. If I say:
f = Frame()
f.grid()
v = IntVar()
v.set(1) # Might help. v.get() at this point returned 0 for me.
 
P

Paul Rubin

Scott David Daniels said:
v.set(1) # Might help. v.get() at this point returned 0 for me.

Yeah, I tried that too. Further reorganization of the code in some
other way suddenly made it start working. I'll have to examine it to
see what I did since I wasn't paying attention to that particular
problem at the time. I had checked sf.net for a bug report on this
and didn't see one, but maybe didn't check carefully enough. I'll
look a little more and then file one, I guess.
 
E

Eric Brunel

Python 2.4, Windows XP. If I say:

f = Frame()
f.grid()
v = IntVar()
c = Checkbutton(f, text='hi there', variable=v)
c.grid()
f.mainloop()

then the checkbutton should initially display as being checked.
But it doesn't. It shows up unchecked, even though v.get() returns 1.
The select operation DOES update the display if there's no variable.

In tcl/tk, booleans are not integers. So using an IntVar to represent something which is basically a boolean will cause unexpected behaviors like the one you're experiencing here. Just do:

....
v = BooleanVar()
v.set(1)
....

and everything should be fine.

HTH
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,053
Latest member
BrodieSola

Latest Threads

Top