pythonttkinter复选框总是返回值为0,即使选中它们也是如此

2024-09-28 18:54:38 发布

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

它应该是一个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)


Tags: textgetvariablepacktomatoestoppingselifintvar
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()
  1. 您的程序中没有myGui.mainloop()。在
  2. TOPPING SELECTION不是有效语句。而且,下面的行不需要是缩进块。在
  3. 不要将tkinter小部件的创建和放置放在一行,否则您将得到一大堆引用None(由pack()返回的值)的变量。在
  4. olivesSelection=a.get()这样的语句并没有像您所想的那样做。该语句调用a.get(),获取返回值0(因为它发生在程序启动时),然后将0分配给olivesSelection。复选框不会更改该变量的值。如果您希望在选中复选框时动态发生,则必须trace()这些IntVar,并将command添加到{}s中
  5. checkToppings()从不调用。在
  6. 检查olivesSelection==1是否总是False,因为a.get()在分配给olivesSelection时是{}(见上文)。在这里使用a.get()。在
  7. 如果在checkToppings()中使用elif,它只会向列表中添加一个topping(第一个有复选框的topping)。在
  8. 对于只有0或{}的值,您不需要使用==1只需说if pepperoniSelection:1是一个真实值,0是一个错误的值(从技术上讲,1实际上是{},而{}实际上是{},因为{}是{}的子类)。在

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()

相关问题 更多 >