Python tkinter复选框始终将值返回为0,即使它们被选中也是如此

时间:2015-05-16 21:44:38

标签: python checkbox tkinter

它应该是一个GUI披萨订单,所以它有更多,但我遇到的问题是当它获得一个应该是橄榄色选择的变量时,它总是在被检查时返回为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)

2 个答案:

答案 0 :(得分:2)

  1. 您的计划中没有myGui.mainloop()
  2. TOPPING SELECTION不是有效的陈述。此外,不需要将以下行作为缩进块。
  3. 不要单行创建和放置tkinter小部件,否则最终会得到一大堆引用None的变量({{1}返回的值}})。
  4. pack()之类的陈述没有按照您的想法行事。该语句调用olivesSelection=a.get(),返回值a.get()(因为它在程序启动时发生),然后将0分配给0。复选框不会更改该变量的值。如果您想要在复选框中动态生成内容,则必须olivesSelection trace()IntVar并将command添加到Checkbutton
  5. checkToppings()永远不会被召唤。
  6. 检查olivesSelection==1是否始终为False,因为a.get()0分配给olivesSelection时为a.get()(见上文)。在这里使用elif
  7. 如果您在checkToppings()中使用==1,则只会在列表中添加一个顶部(第一个带有复选框的顶部)。
  8. 您不需要使用0来获取仅1if pepperoniSelection:的值 - 只需说10是一个真值,而1是一个假值(技术上True实际上是0False实际上是bool,因为{{1} }}是int)的子类。
  9. 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()
    

答案 1 :(得分:0)

好的,所以有几件事。

您需要在定义中使用topping.get()来调用浇头。否则,您只是在运行程序时执行.get()而不是按需执行

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

elif树对您没有帮助,因为只要您发出ifTrue之一,您就会退出{{1}阻止。此外,作为个人偏好和代码清洁度的问题,您可能需要查找字典。一旦你在一个区块中过去3或4个if语句,它们就会变得相当混乱。

另外,我知道你说有更多的代码,但是如果您忘记了代码的其余部分,请务必提醒您,请确保让您的主窗口实例输入if < / p>

mainloop()