Checkbutton状态检查Python Tkin

2024-04-26 10:33:29 发布

您现在位置:Python中文网/ 问答频道 /正文

Hi-im正在尝试在python上编写一段代码,根据选中的checkbutton的数量列出标签中的值。为什么我的代码不能工作?在

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)

谢谢:)


Tags: andtheifcheckwindowpackevenodd
2条回答

你需要使用变量获取(). 下面是Python3.3中的一个工作示例。在

from tkinter import *

root=Tk()

class CheckB():
    def __init__(self, master, text):
        self.var = IntVar()
        self.text=text
        c = Checkbutton(
            master, text=text,
            variable=self.var,
            command=self.check)
        c.pack()

    def check(self):
        print (self.text, "is", self.var.get())


check1 = CheckB(root, text="Gamma")
check2 = CheckB(root, text="Beta")
check3 = CheckB(root, text="Alpha")

希望能帮上忙!-埃德

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

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

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

相关问题 更多 >