How do I get the 'state'
of a Tkinter Checkbutton
? By 'state'
I mean get whether or not it has a check mark in it or not.

- 14,998
- 7
- 24
- 51

- 50,393
- 94
- 205
- 275
3 Answers
When you're 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

- 68,820
- 20
- 127
- 125
-
4Not possible without variable? =( – Ciro Santilli OurBigBook.com Jan 15 '14 at 09:29
-
7if its important to you that you can get the value via the widget you can always attach the variable to the widget (something like chk.val = var) and then access it with chk.val.get() – Ronen Ness Dec 06 '14 at 01:11
-
1@CiroSantilli烏坎事件2016六四事件法轮功 It's many years later, but I answered a comment you had a long time ago. – bitsmack Dec 11 '16 at 09:08
-
3Using variables is actually quite powerful. Not only can you use the variable to check the status of the checkbox, but you can also use the variable to SET the value of the checkbox. So, var.set(1) will make sure that the check mark is shown, and var.set(0) will make sure the check is cleared. Further, you can set a trace function for the variable, so that if someone clicks on the checkbox, that function can be triggered. It's all part of the event-driven paradigm. Very powerful stuff. – GaryMBloom Nov 07 '17 at 19:03
If you 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)
Notice that the new checkbox defaults to the "alternate", sometimes called "half-checked", state:
You can read the current state usinge the .state() method:
>>> print(chk.state()) # half-checked
('alternate',)
>>> print(chk.state()) # checked
('selected',)
>>> print(chk.state()) # not checked
()
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:
chk.state(['!alternate'])
If you disable/enable the checkbutton using
chk.state(['disabled']) chk.state(['!disabled'])
then everything works fine. But, if you use these common, alternate methods:
chk.config(state=tk.DISABLED) chk.config(state=tk.NORMAL)
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 :)
* ttk became available in Python 2.7 (Tk 8.5). This question talks about the differences between the old standard widgets and the newer, "themed" ones.
-
2
-
4@AbdullahSaid Hmm, it still works for me. Perhaps you are using the old-style tkinter Checkbutton instead of `ttk.Checkbutton`? – bitsmack May 17 '20 at 20:10
bitsmack answer doesn't exactly agree with what I am seeing.
chk.state() returns a tuple which has two elements when selected: ('focus','selected')
import tkinter as tk
from tkinter import ttk
def p(event):
st = ck.state()
print (f'is tuple {type(st) is tuple} {len(st)}-----------------------------------------')
if 'selected' in st:
print ('got selected')
for i,pst in enumerate(st) :
print(f'{i}, {pst}')
root = tk.Tk()
root.geometry('200x200+300+200')
root.grid_rowconfigure(0, weight = 1)
ck = ttk.Checkbutton(root,text = 'tryme')
ck.grid()
root.bind('<Return>',p)
root.mainloop()
yields this result:
is tuple True 1----------------------------------------
0, alternate is tuple True 2-----------------------------------------
got selected 0, focus 1, selected is tuple True 1-----------------------------------------
0, focus
So, to determine if the checkbox is selected:
if 'selected' in chk.state()
for alternate:
if 'alternate' in chk.state()
for not selected:
if not ('selected' in chk.state or 'alternate' in chk.state)

- 189
- 1
- 10
-
I know I shouldn't refer to a previous answer, but I cannot add comments at this time – Jim Robinson Jan 22 '21 at 22:26
-
Hello Jim. Welcome to Stack Overflow, and thank you for contributing. I've downvoted this answer because it reads like a response to an existing answer, and a popular solution has already been accepted on this 10-year-old question. Please consider deleting this answer and instead leaving a comment on @bitsmack's answer – Captain Hat Jan 23 '21 at 01:33
-
5Thank you for your comment. I would have commented on the answer, but i lack the reputation to comment. I felt that the problems I encountered following the answer were significant enough to need clarification – Jim Robinson Jan 23 '21 at 04:23