【问题标题】:Python tkinter checkboxes always returning value as 0 even when they are checkedPython tkinter 复选框即使被选中也总是返回值为 0
【发布时间】:2015-05-17 07:07:19
【问题描述】:

它应该是一个 GUI 披萨订单表格,所以它有更多但我遇到的问题是当它获取一个应该是 OliveSelection 的变量时,它总是在检查时返回 0 而不是 1。

from tkinter import *
myGui=Tk()
myGui.geometry("800x600")
myGui.title("Pete's Pizza Parlour~Order Form")


TOPPING SELECTION
    toppings_lbl=Label(myGui,text="Toppings:",font=("Good Times",10),fg="blue").pack()
    a=IntVar()
    olives_chk=Checkbutton(myGui,text="Olives",variable=a).pack()
    b=IntVar()
    tomatoes_chk=Checkbutton(myGui,text="Tomatoes",variable=b).pack()
    c=IntVar()
    pepperoni_chk=Checkbutton(myGui,text="Pepperoni",variable=c).pack()
    d=IntVar()
    hotPeppers_chk=Checkbutton(myGui,text="Hot Peppers",variable=d).pack()
    e=IntVar()
    onions_chk=Checkbutton(myGui,text="Onions",variable=e).pack()
    f=IntVar()
    ham_chk=Checkbutton(myGui,text="Ham",variable=f).pack()
    g=IntVar()
    sausage_chk=Checkbutton(myGui,text="Sausage",variable=g).pack()
    h=IntVar()
    greenPeppers_chk=Checkbutton(myGui,text="Green Peppers",variable=h).pack()

olivesSelection=a.get()
tomatoesSelection=b.get()
pepperoniSelection=c.get()
hotPeppersSelection=d.get()
onionsSelection=e.get()
hamSelection=f.get()
sausageSelection=g.get()
greenPeppersSelection=h.get()

olivesSelectionStr="olives"
tomatoesSelectionStr="tomatoes"
pepperoniSelectionStr="pepperoni"
hotPeppersSelectionStr="hot peppers"
onionsSelectionStr="onions"
hamSelectionStr="ham"
sausageSelectionStr="sausage"
greenPeppersSelectionStr="green peppers"
noToppingsStr="no toppings."
def checkToppings():
    toppingsList=""
    if(olivesSelection==1):
        toppingsList=toppingsList+olivesSelectionStr
    elif(tomatoesSelection==1):
        toppingsList=toppingsList+tomatoesSelectionStr
    elif(pepperoniSelection==1):
        toppingsList=toppingsList+pepperoniSelectionStr
    elif(hotPeppersSelection==1):
        toppingsList=toppingsList+hotPeppersSelectionStr
    elif(onionsSelection==1):
        toppingsList=toppingsList+onionsSelectionStr
    elif(hamSelection==1):
        toppingsList=toppingsList+hamSelectionStr
    elif(sausageSelection==1):
        toppingsList=toppingsList+sausageSelectionStr
    elif(greenPeppersSelection==1):
        toppingsList=toppingsList+greenPeppersSelectionStr
    else:
        toppingsList=noToppingsStr

橄榄选择总是返回 0 而不是 1 当它应该被选中时 print(olivesSelection)

【问题讨论】:

    标签: python checkbox tkinter


    【解决方案1】:
    1. 您的程序中没有myGui.mainloop()
    2. TOPPING SELECTION 不是有效的声明。此外,以下行不需要缩进。
    3. 不要单行创建和放置tkinter 小部件,否则最终会得到一大堆引用None 的变量(pack() 返回的值)。李>
    4. olivesSelection=a.get() 之类的声明并没有按照您的想法行事。该语句调用a.get(),获得返回值0(因为它发生在程序启动时),然后将0 分配给olivesSelection。复选框不会更改该变量的值。如果您希望在选中复选框时动态发生事情,您必须 trace() 那些 IntVars 并将 commands 添加到 Checkbuttons。
    5. checkToppings() 永远不会被调用。
    6. 检查olivesSelection==1 是否总是False,因为a.get() 在分配给olivesSelection 时是0(见上文)。在此处使用a.get()
    7. 如果您在checkToppings() 中使用elif,它只会在列表中添加一个顶部(第一个带有复选框的顶部)。
    8. 您不需要将==1 用于仅是01 的值 - 只需说if pepperoniSelection:1 是真值,0 是假值(从技术上讲,1 实际上是 True0 实际上是 False,因为 boolint 的子类)。

    from tkinter import *
    myGui=Tk()
    myGui.geometry("800x600")
    myGui.title("Pete's Pizza Parlour~Order Form")
    
    
    def checkem():
        print(a.get(), b.get(), c.get(), d.get(), e.get(), f.get(), g.get(), h.get())
        print(checkToppings())
    
    toppings_lbl=Label(myGui,text="Toppings:",font=("Good Times",10),fg="blue")
    toppings_lbl.pack()
    a=IntVar()
    olives_chk=Checkbutton(myGui,text="Olives",variable=a)
    olives_chk.pack()
    b=IntVar()
    tomatoes_chk=Checkbutton(myGui,text="Tomatoes",variable=b)
    tomatoes_chk.pack()
    c=IntVar()
    pepperoni_chk=Checkbutton(myGui,text="Pepperoni",variable=c)
    pepperoni_chk.pack()
    d=IntVar()
    hotPeppers_chk=Checkbutton(myGui,text="Hot Peppers",variable=d)
    hotPeppers_chk.pack()
    e=IntVar()
    onions_chk=Checkbutton(myGui,text="Onions",variable=e)
    onions_chk.pack()
    f=IntVar()
    ham_chk=Checkbutton(myGui,text="Ham",variable=f)
    ham_chk.pack()
    g=IntVar()
    sausage_chk=Checkbutton(myGui,text="Sausage",variable=g)
    sausage_chk.pack()
    h=IntVar()
    greenPeppers_chk=Checkbutton(myGui,text="Green Peppers",variable=h)
    greenPeppers_chk.pack()
    thebutton = Button(myGui, text='check', command=checkem)
    thebutton.pack()
    
    olivesSelectionStr="olives"
    tomatoesSelectionStr="tomatoes"
    pepperoniSelectionStr="pepperoni"
    hotPeppersSelectionStr="hot peppers"
    onionsSelectionStr="onions"
    hamSelectionStr="ham"
    sausageSelectionStr="sausage"
    greenPeppersSelectionStr="green peppers"
    noToppingsStr="no toppings."
    
    def checkToppings():
        toppings = [var for val,var in zip((a.get(), b.get(), c.get(), d.get(),
                                            e.get(), f.get(), g.get(), h.get()),
                                           (olivesSelectionStr, tomatoesSelectionStr,
                                            pepperoniSelectionStr, hotPeppersSelectionStr,
                                            onionsSelectionStr, hamSelectionStr,
                                            sausageSelectionStr, greenPeppersSelectionStr))
                    if val]
        if not toppings:
            return 'no toppings.'
        elif len(toppings)==1:
            return toppings[0] + '.'
        elif len(toppings)==2:
            return ' and '.join(toppings) + '.'
        else:
            return ', '.join(toppings[:-1]) + ', and ' + toppings[-1] + '.'
    
    myGui.mainloop()
    

    【讨论】:

    • 第一次使用这个网站,我是 python 和一般编程的新手,这真的很有帮助,谢谢 :))
    【解决方案2】:

    好的,有几件事。

    您需要在需要浇头的定义中包含 topping.get()。否则你只是在运行程序时执行.get() 而不是按需执行

    TOPPING SELECTION 应该被注释掉,因为它没有其他用途

    elif 大树不会对您有帮助,因为一旦您做出if 语句之一True,您将退出if 块。此外,作为个人喜好和代码清洁度的问题,您可能需要为此查找字典。一旦你在一个块中超过 3 或 4 个 if 语句,它们就会变得相当混乱。

    另外,我知道你说还有更多代码,但作为一个友好的提醒,以防你忘记了其余代码的底部,请务必让你的主窗口实例输入mainloop()

    from tkinter import *
    myGui=Tk()
    myGui.geometry("800x600")
    myGui.title("Pete's Pizza Parlour~Order Form")
    
    #TOPPING SELECTION
    toppings_lbl=Label(myGui,text="Toppings:",font=("Good Times",10),fg="blue").pack()
    a=IntVar()
    olives_chk=Checkbutton(myGui,text="Olives",variable=a).pack()
    b=IntVar()
    tomatoes_chk=Checkbutton(myGui,text="Tomatoes",variable=b).pack()
    c=IntVar()
    pepperoni_chk=Checkbutton(myGui,text="Pepperoni",variable=c).pack()
    d=IntVar()
    hotPeppers_chk=Checkbutton(myGui,text="Hot Peppers",variable=d).pack()
    e=IntVar()
    onions_chk=Checkbutton(myGui,text="Onions",variable=e).pack()
    f=IntVar()
    ham_chk=Checkbutton(myGui,text="Ham",variable=f).pack()
    g=IntVar()
    sausage_chk=Checkbutton(myGui,text="Sausage",variable=g).pack()
    h=IntVar()
    greenPeppers_chk=Checkbutton(myGui,text="Green Peppers",variable=h).pack()
    
    olivesSelectionStr="olives"
    tomatoesSelectionStr="tomatoes"
    pepperoniSelectionStr="pepperoni"
    hotPeppersSelectionStr="hot peppers"
    onionsSelectionStr="onions"
    hamSelectionStr="ham"
    sausageSelectionStr="sausage"
    greenPeppersSelectionStr="green peppers"
    noToppingsStr="no toppings."
    
    
    def checkToppings():
        toppingsList=""
        olivesSelection=a.get()
        tomatoesSelection=b.get()
        pepperoniSelection=c.get()
        hotPeppersSelection=d.get()
        onionsSelection=e.get()
        hamSelection=f.get()
        sausageSelection=g.get()
        greenPeppersSelection=h.get()
        if(olivesSelection==1):
            toppingsList=toppingsList+olivesSelectionStr
        if(tomatoesSelection==1):
            toppingsList=toppingsList+tomatoesSelectionStr
        if(pepperoniSelection==1):
            toppingsList=toppingsList+pepperoniSelectionStr
        if(hotPeppersSelection==1):
            toppingsList=toppingsList+hotPeppersSelectionStr
        if(onionsSelection==1):
            toppingsList=toppingsList+onionsSelectionStr
        if(hamSelection==1):
            toppingsList=toppingsList+hamSelectionStr
        if(sausageSelection==1):
            toppingsList=toppingsList+sausageSelectionStr
        if(greenPeppersSelection==1):
            toppingsList=toppingsList+greenPeppersSelectionStr
        if toppingsList=="":
            toppingsList=noToppingsStr
        print(toppingsList)
    
    topping_btn = Button(myGui,text='print toppings', command = checkToppings)
    topping_btn.pack()
    myGui.mainloop()
    

    【讨论】:

    • toppings_lbl=Label(...).pack(...) 并没有像你想象的那样做:变量将被设置为None,因为pack() 返回None
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-02
    • 2019-11-17
    • 2021-12-26
    • 2018-09-26
    • 2013-10-29
    • 2011-12-02
    相关资源
    最近更新 更多