gpt4 book ai didi

python - 检查按钮状态检查 Python Tkinter

转载 作者:太空宇宙 更新时间:2023-11-04 10:35:42 24 4
gpt4 key购买 nike

您好,我正在尝试在 python 上编写一段代码,根据选中的复选按钮的数量在标签中列出一个值。为什么我的代码不起作用?

var1=IntVar()
var2=IntVar()
var3=IntVar()



inlabel = StringVar()


label = Label (the_window, height = 1,bg = "white",width = 30, textvariable = inlabel,font = ("arial",50,"normal")).pack()
def check():
if(var1 == 0 and var2 == 0 and var3==1):
inlabel.set("odd")
if(var1 == 0 and var2 == 1 and var3==1):
inlabel.set("even")
if(var1 == 1 and var2 == 1 and var3==1):
inlabel.set("odd")
if(var1 == 0 and var2 == 1 and var3==0):
inlabel.set("odd")
if(var1 == 1 and var2 == 1 and var3==0):
inlabel.set("even")
if(var1 == 0 and var2 == 0 and var3==0):
inlabel.set("null")
if(var1 == 1 and var2 == 0 and var3==0):
inlabel.set("odd")
if(var1 == 1 and var2 == 0 and var3==1):
inlabel.set("even")

check1 = Checkbutton(the_window, text= "Gamma",variable=var1,command=check)
check2 = Checkbutton(the_window, text= "Beta",variable=var2,command=check)
check3 = Checkbutton(the_window, text= "Alpha",variable=var3,command=check)
check1.pack(side=RIGHT)
check2.pack()
check3.pack(side=LEFT)

谢谢:)

最佳答案

您需要使用get 并将其分配给另一个变量而不是var1。所以这样的事情应该有效。

value1 = var1.get()
value2 = var2.get()
value3 = var3.get()

我假设您在实际程序中定义了父窗口 (the_window)。

关于python - 检查按钮状态检查 Python Tkinter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23453085/

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