它应该是一个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)
好吧,有几件事。在
你需要把你的
topping.get()
放在你的定义里面,这个定义需要添加配料。否则,您只是在运行程序时执行.get()
,而不是按需执行TOPPING SELECTION
应该被注释掉,因为它没有其他用途大的
elif
树不会对您有帮助,因为一旦您执行if
语句True
,您就要退出if
块。另外,出于个人喜好和代码清洁度的考虑,您可能需要查找字典来了解这一点。一旦超过块中的3或4条if
语句,它们就会变得相当混乱。在另外,我知道您说过还有更多的代码,但是作为一个友好的提醒,以防您忘记了其余代码的底部,请确保您的主窗口实例输入
mainloop()
myGui.mainloop()
。在TOPPING SELECTION
不是有效语句。而且,下面的行不需要是缩进块。在tkinter
小部件的创建和放置放在一行,否则您将得到一大堆引用None
(由pack()
返回的值)的变量。在olivesSelection=a.get()
这样的语句并没有像您所想的那样做。该语句调用a.get()
,获取返回值0
(因为它发生在程序启动时),然后将0
分配给olivesSelection
。复选框不会更改该变量的值。如果您希望在选中复选框时动态发生,则必须trace()
这些IntVar
,并将command
添加到{checkToppings()
从不调用。在olivesSelection==1
是否总是False
,因为a.get()
在分配给olivesSelection
时是{a.get()
。在checkToppings()
中使用elif
,它只会向列表中添加一个topping(第一个有复选框的topping)。在0
或{==1
只需说if pepperoniSelection:
。1
是一个真实值,0
是一个错误的值(从技术上讲,1
实际上是{相关问题 更多 >
编程相关推荐