binnev
binnev

Reputation: 1858

Tkinter Checkbutton not working

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.

Upvotes: 5

Views: 8253

Answers (4)

Federico David
Federico David

Reputation: 83

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()

Upvotes: 0

Gaurav Nagar
Gaurav Nagar

Reputation: 381

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)

Upvotes: 1

binnev
binnev

Reputation: 1858

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()

Upvotes: 1

Module_art
Module_art

Reputation: 1068

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()

Upvotes: 0

Related Questions