4

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: See behaviour here

I don't know what I am doing wrong. I know I have avoided the following pitfalls:

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.
binnev
  • 1,658
  • 1
  • 14
  • 16
  • I get expected output in 3.2 python, Tk version 8.5 – Artemis May 10 '18 at 15:18
  • Please include your import statements. We can't tell for certain if you're using the tkinter Checkbutton or the ttk Checkbutton. – Bryan Oakley May 10 '18 at 15:19
  • @BryanOakley thanks, added the import statements to the question. – binnev May 10 '18 at 15:20
  • 1
    The code in that little video is not the same as the code posted in your question. At least one difference is that this code uses `self.c` whereas the code in the video just uses `c`. I don't think that matters, but you need to make sure that the code posted is an accurate reflection of your real code. – Bryan Oakley May 10 '18 at 15:21
  • The code in your question won't run. The import statement has a syntax error. Please provide actual code that you're running on your system. Though, even when I fix that problem, the code works fine for me. – Bryan Oakley May 10 '18 at 15:23
  • @BryanOakley yes, I updated it to `self.c` after the video (it didn't change anything) – binnev May 10 '18 at 15:23
  • @BryanOakley removed trailing comma from import statement (I was importing Button as well in my code, but trimmed it off for a MWE here). – binnev May 10 '18 at 15:24
  • @ArtemisFowl thanks for testing. I've added my python version to the question. – binnev May 10 '18 at 15:25
  • Your code works for me with cpython. Perhaps the problem is a bug in Anaconda. – Bryan Oakley May 10 '18 at 15:26
  • @BryanOakley could you please tell me what tk version you have? I have 8.5.18 – binnev May 10 '18 at 15:29
  • I've tested with 3.5.2 and 2.7.12 – Bryan Oakley May 10 '18 at 15:32
  • Thanks for your help Bryan. I've updated my tk (to v8.6.7) but still no luck. – binnev May 10 '18 at 15:47
  • Side note. `root.destroy()` is pointless here. Once you close the program there will not be any `root` to `destroy()`. You can delete that line. – Mike - SMT May 10 '18 at 19:14
  • I've tested in python 3.6.5 Tk 8.6.6, still expected result. – Artemis May 10 '18 at 19:21

4 Answers4

1

In case anyone has the same problem and needs a quick fix, here's my hacky solution:

from tkinter import Tk, Checkbutton, IntVar
class MyGUI:
    def __init__(self, window):
        self.var = IntVar()
        self.c = Checkbutton(
            window, text="Enable Tab",
            command=lambda:self.toggle(self.var))
        self.c.pack()

    def toggle(self, var):
        var.set(not var.get())

root = Tk()
gui = MyGUI(root)
root.mainloop()
binnev
  • 1,658
  • 1
  • 14
  • 16
  • 1
    Remove the last line. `root.destroy()`. It is pointless here and will throw an error when your program closes. – Mike - SMT May 10 '18 at 19:15
1
chkValue = BooleanVar(root) 
chkValue.set(True)

chk = Checkbutton(root, text=' Remember Password',var=chkValue) 
chk.grid(column=1,row=3,sticky=W)

use master

  • BooleanVar(master)
  • IntVar(master)
  • StringVar(master)
Gaurav Nagar
  • 191
  • 2
  • 4
0

Your code works for me.

Maybe try to import everything from Tkinter* and delete the last line. But I think your problem is an anaconda bug.

from tkinter import *
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())

if __name__ == "__main__":
    root = Tk()
    gui = MyGUI(root)
    root.mainloop()
Artemis
  • 2,553
  • 7
  • 21
  • 36
Module_art
  • 999
  • 2
  • 9
  • 26
0

this is my solution i had to do it like this you had the same problem calling inside and importing from a main file and using the class

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):
        if self.var.get() == 0:
           self.var.set(1)
        else:
           self.var.set(0)
        print("self.var is", self.var.get())

root = Tk()
gui = MyGUI(root)
root.mainloop()