How to get state of Checkbutton when it's selected?

不羁的心 提交于 2020-02-22 05:03:51

问题


How can I get the state of a Checkbutton in python? I have this:

def doSomething():

  if #code goes here, if checkbutton is selected
   ...

check = Checkbutton(window, text="Add both", onvalue = 1, offvalue = 0)
check.pack(side="right")

回答1:


You need to associate the Checkbox with a variable:

is_checked = IntVar()
check = Checkbutton(window, text="Add both", onvalue=1, offvalue=0, variable=is_checked)

Then utilise do your check such as:

if is_checked.get():
    # do something


来源:https://stackoverflow.com/questions/16778424/how-to-get-state-of-checkbutton-when-its-selected

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!