【问题标题】:How to get state of Checkbutton when it's selected?选中时如何获取 Checkbutton 的状态?
【发布时间】:2013-05-22 14:52:50
【问题描述】:

如何在 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")

【问题讨论】:

标签: python checkbox tkinter


【解决方案1】:

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

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

然后使用进行检查,例如:

if is_checked.get():
    # do something

【讨论】:

  • 把我搞砸了两个小时,因为缺少这个答案.get()
  • 如果你不想为复选框创建新变量怎么办?
猜你喜欢
  • 2021-02-06
  • 2012-02-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-23
相关资源
最近更新 更多