它应该是GUI Pizza的订购单,所以它还有更多,但是我遇到的问题是,当它得到一个应该是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)
最佳答案
您的程序中没有myGui.mainloop()
。TOPPING SELECTION
不是有效的语句。同样,也不需要将以下几行缩进。
不要一行一行地创建和放置tkinter
小部件,否则最终会得到一堆引用None
的变量(pack()
返回的值)。
像olivesSelection=a.get()
这样的语句并没有按照您认为的方式执行。该语句调用a.get()
,获得返回值0
(因为它在程序启动时发生),然后将该0
分配给olivesSelection
。复选框不会更改该变量的值。如果希望事情随着复选框而动态发生,则必须trace()
这些IntVar
并将command
添加到Checkbutton
中。
永远不会调用checkToppings()
。
检查olivesSelection==1
是否总是False
,因为a.get()
在分配给0
时为olivesSelection
(请参见上文)。在此处使用a.get()
。
如果在elif
中使用checkToppings()
,则只会在列表中添加一个顶部(第一个具有复选框的顶部)。
您不需要将==1
用作仅是0
或1
的值-只需说if pepperoniSelection:
。 1
是真实值,而0
是虚假值(从技术上讲,1
实际上是True
,而0
实际上是False
,因为bool
是int
的子类)。
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()