Getting Tkinter Check Box State

Getting Tkinter Check Box State

Asked on November 22, 2018 in tkinter.
Add Comment


  • 3 Answer(s)

    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
    
    Answered on November 22, 2018.
    Add Comment

    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:

    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

    Answered on November 22, 2018.
    Add Comment

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

    Answered on April 17, 2019.
    Add Comment


  • Your Answer

    By posting your answer, you agree to the privacy policy and terms of service.