Python:如果选中了checkbutton

IAM*_*IAM 4 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")
Run Code Online (Sandbox Code Playgroud)

Jon*_*nts 7

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

is_checked = IntVar()
check = Checkbutton(window, text="Add both", onvalue=1, offvalue=0, variable=is_checked)
Run Code Online (Sandbox Code Playgroud)

然后利用你的支票,如:

if is_checked.get():
    # do something
Run Code Online (Sandbox Code Playgroud)