Getting Checkbutton state

前端 未结 2 2014
生来不讨喜
生来不讨喜 2020-11-28 10:00

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.

相关标签:
2条回答
  • 2020-11-28 10:14

    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:

    1. 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'])
      
    2. 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.

    0 讨论(0)
  • 2020-11-28 10:23

    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
    
    0 讨论(0)
提交回复
热议问题