Getting Tkinter Check Box State
The answer is:
When creating it, it takes a variable keyword argument. Pass it an IntVar from Tkinter. Checking or unchecking the box will set that value contained by var to the corresponding boolean state. This can be accessed as var.get():
checked => var.get()
not checked => not var.get()
>>> root = Tkinter.Tk() >>> var = Tkinter.IntVar() >>> chk = Tkinter.Checkbutton(root, text='foo', variable=var) >>> chk.pack(side=Tkinter.LEFT) >>> var.get() #unchecked 0 >>> var.get() #checked 1
The best solution is:
I use the new* ttk module from tkinter, you can read and write checkbutton states without assigning variables.
import tkinter from tkinter import ttk tkwindow = tkinter.Tk() chk = ttk.Checkbutton(tkwindow, text="foo") chk.grid(column=0, row=0)
I Notice that the new checkbox defaults to the “alternate”, sometimes called “half-checked”, state:
You can read the current state usinge the .state() method:
To set the state in code: chk.state(['selected']) # check the checkbox chk.state(['!selected']) # clear the checkbox chk.state(['disabled']) # disable the checkbox chk.state(['!disabled','selected']) # enable the checkbox and put a check in it!
And here is a convenient way to check for a specific state:
chk.instate(['selected']) # returns True if the box is checked
There are two tricky things I’ve found
The initial state is “alternate”, and this state flag doesn’t get cleared when adding a “selected” state flag. So, if you want to toggle your checkbutton in code, you’ll first need to clear the “alternate” flag:
If you disable/enable the checkbutton using
then everything works fine. But, if you use these common, alternate methods:
then it reasserts the ‘alternate’ flag.
This behavior doesn’t happen if you assign a variable to the checkbutton, but then, if you wanted to assign a variable then this answer probably won’t help you