gpt4 book ai didi

python-3.x - 当我检查另一个 checkButton 时,如何检查两个 python tkinter checkButtons?

转载 作者:行者123 更新时间:2023-12-05 02:27:26 38 4
gpt4 key购买 nike

我是 tkinter 的新手,在使用 checkButton() 时遇到了问题。我想要它,这样当我检查“按钮 3”时,“按钮 1”和“按钮 2”也会被检查。到目前为止,这是我的代码。我看了这个问题:Can i make one checkbutton in tkinter check all the other checkbuttons? ,这导致我使用 .select() 方法,但是当我运行 GUI 并检查按钮 3 时,按钮 1 和 2 不会被检查。我应该如何编写函数,以便当我选中“按钮 3”时,“按钮 1”和“按钮 2”也被选中?

from tkinter import *

root = Tk()

button1_bool = BooleanVar()
button1 = checkButton(root, text = "Button 1", variable = button1_bool, onvalue = True, offvalue = False)
button1.pack()

button2_bool = BooleanVar()
button2 = checkButton(root, text = "Button 2", variable = button2_bool, onvalue = True, offvalue = False)
button2.pack()

def button3ischecked():
if button3.get() == True:
button1.select()
button2.select()

button3_bool = BooleanVar()
button3 = checkButton(root, text = "Button 3", variable = button3_bool, onvalue = True, offvalue = False, command = button3ischecked)

root.mainloop()

最佳答案

您不使用 .get() 来检查复选框的状态 - 只需评估分配给它的 bool 变量。

from tkinter import *

root = Tk()

button1_bool = BooleanVar()
button1 = Checkbutton(root, text = "Button 1", variable = button1_bool, onvalue = True, offvalue = False)
button1.pack()

button2_bool = BooleanVar()
button2 = Checkbutton(root, text = "Button 2", variable = button2_bool, onvalue = True, offvalue = False)
button2.pack()

def button3ischecked():
if button3_bool:
button1.select()
button2.select()

button3_bool = BooleanVar()
button3 = Checkbutton(root, text = "Button 3", variable = button3_bool, onvalue = True, offvalue = False, command = button3ischecked)
button3.pack()

root.mainloop()

希望这对您有所帮助。

关于python-3.x - 当我检查另一个 checkButton 时,如何检查两个 python tkinter checkButtons?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73229103/

38 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com