Python:如果选中了checkbutton

时间:2013-05-27 18:07:32

标签: python checkbox tkinter

如何在python中获取checkbutton的状态?我有这个:

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 个答案:

答案 0 :(得分:7)

您需要将Checkbox与变量相关联:

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

然后利用你的支票,如:

if is_checked.get():
    # do something