首页 > 解决方案 > 使用 CheckButton 激活或停用按钮

问题描述

我创建了这个程序,但如果我们不选择 checkButton(第二部分),我希望禁用按钮“按钮”(第一部分)。所以我想只有在选中 CheckButton 时才激活按钮。

acceptButton = IntVar()
case = Checkbutton(fenetre, text="I accept the agreement", variable=acceptButton, cursor="hand2", bg="white",)
case.pack()
var = case.select() == 1

doNotButton = IntVar()
case2 = Checkbutton(fenetre, text="I do not accept the agreement", variable=doNotButton, bg="white", state=DISABLED) #il est impossible de cocher cette case
case2.pack()

按钮:

button = Button(fenetre, text="Suivant", command=commandSuiv, cursor="hand2", height=1, width = 15) #taille
button.pack()
button.place(x=c, y=d)
button.configure(font=f)

太感谢了 :)

标签: pythontkinteractivation

解决方案


您可以先检查 CheckButton 变量,然后相应地设置 Button 的状态:

if acceptButton.get() == 0: # Your binded var returns 0 if unchecked, 1 otherwise
    button.config(state="disabled")

您也可以通过默认禁用按钮来反转该过程,它可能会更优雅:

button = Button(fenetre, text="Suivant", command=commandSuiv, cursor="hand2", height=1, width = 15, state=DISABLED) #taille

然后启用它,它将是:

if acceptButton.get() == 1:
    button.config(state="normal")

推荐阅读