pythontkintertkinter.checkbutton

Getting Checkbutton state


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.


Solution

  • 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