Getting Checkbutton state

前端 未结 2 2013
生来不讨喜
生来不讨喜 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: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
    

提交回复
热议问题