【问题标题】:ttk checkbutton is not deselected by defaultttk checkbutton 默认不取消选中
【发布时间】:2020-06-15 11:58:06
【问题描述】:

Please kick here to see the current output and expected output

我有一个简单的 python 程序,我想默认取消选中复选按钮。我希望以与用户取消选中复选框时相同的方式查看它。请告诉我如何实现它。

from tkinter import *
from tkinter import ttk

def urgentReq():
    global box
    state = box.state()
    if(box.instate(['selected'])):        
        print ("--> Urgent: ",state)
    else:
        print ("--> Not Urgent:",state)

gui = Tk()
gui.title("GUI")
gui.geometry('200x150')

box = ttk.Checkbutton(gui, text ='Urgent Request', command=lambda: urgentReq())
box.grid(column=1, row=4, pady=40, sticky="N")
#write something here to unselect the box by default

【问题讨论】:

  • 当我运行您的代码时,Checkbutton 最初未选中。

标签: python tkinter ttk tkinter.checkbutton ttkwidgets


【解决方案1】:

使用 .invoke() 方法,但从我在别处读到的内容,如果有关联,也会调用该命令。我尝试使用它的实例,我的检查按钮没有命令作为参数,所以这对我来说非常有效。

希望这会有所帮助,祝你好运!

【讨论】:

    【解决方案2】:
    box.state(['!alternate'])  #box appear unchecked
    box.state(['selected'])  #box appear checked
    

    【讨论】:

    • box.state(['!alternate']) 已解决问题。
    【解决方案3】:

    CheckButton 的初始状态不知何故 = ('alternate',)

    我在这里找到了一种解决方法:tkk checkbutton appears when loaded up with black box in it。如果你像这样将它应用到你的代码中,它似乎可以工作:

    checkVar = IntVar()
    box = ttk.Checkbutton(gui, text ='Urgent Request', command=lambda: urgentReq(), variable=checkVar)
    

    【讨论】:

      【解决方案4】:

      我想你可以写box.deselect() 来取消选择它。 编辑:糟糕,我刚刚测试了它,它不适用于 ttk ......对不起。 :)

      【讨论】:

      • 我认为Checkbutton 对象没有deselect() 方法
      猜你喜欢
      • 2013-01-08
      • 2021-07-21
      • 1970-01-01
      • 1970-01-01
      • 2020-12-09
      • 2015-11-05
      • 2013-09-26
      • 2015-03-12
      • 2017-11-01
      相关资源
      最近更新 更多