I know this will seem like a silly question, but I have read all the related/similar questions I could find, and I am pretty sure I am experiencing a different issue. See the end of this question for a list of similar problems which I have avoided.
I am trying to use a Tkinter Checkbutton. I have used the example code from the documentation (here) almost verbatim.
from tkinter import Tk, Checkbutton, IntVar
class MyGUI:
def __init__(self, window):
self.var = IntVar()
self.c = Checkbutton(window,
text="Enable Tab",
variable=self.var,
command=self.cb)
self.c.pack()
def cb(self):
print("self.var is", self.var.get())
root = Tk()
gui = MyGUI(root)
root.mainloop()
root.destroy()
The only thing I changed was to remove the event
argument from the cb
method, because as far as I could tell it wasn't being used for anything, and the checkbutton doesn't pass any event
to cb
.
My problem is that the variable storing the checkbutton's value always reads 0
, even when the checkbutton is checked:
I don't know what I am doing wrong. I know I have avoided the following pitfalls:
- Using the wrong variable -- I have used Tk's
IntVar
, not a pythonint
- Using daft colours
- Using the same attribute name for the button and its variable -- I have saved the button and the variable as separate attributes to my class.
- Chaining the Checkbox creation with the place method and accidentally storing
None
Also, when I run code from a question with a similar issue, I get the same behaviour -- checkbox always returns False/0 -- even though that question is marked as resolved.
I am using Anaconda python with the following versions:
Python 3.5.4 |Anaconda custom (64-bit)| (default, Nov 8 2017, 14:34:30)
[MSC v.1900 64 bit (AMD64)]
IPython 6.2.1 -- An enhanced Interactive Python.