Getting Checkbutton state

rectangletangle picture rectangletangle · Nov 21, 2010 · Viewed 88.7k times · Source

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.

Answer

aaronasterling picture aaronasterling · Nov 21, 2010

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