在我的代码中,我想通过将要购买的饮料数量加起来来工作,但是在尝试添加代码时,如果他们在外卖输入框中输入yes,它将增加总数的5%。 我已经使用if语句在def中添加了它,但是似乎并没有注册该定义的存在。我在它之后添加了一个print('hi'),甚至没有显示出来。我是python的新手,看了很多之后,我不知道该怎么做。我也知道我的代码可能真的组织得很烂,凌乱,但我不知道如何解决。任何帮助将不胜感激。
这是我的整个代码,时间不太长,主要问题是在运行程序时def Takeaway()未注册。
from tkinter import*
import random
import time
import datetime
root=Tk()
root.geometry("1600x8000")
root.title("Cafe au Lait")
Tops=Frame(root, width=1600,relief=SUNKEN)
Tops.pack(side=TOP)
f1=Frame(root,width=800,height=700,relief=SUNKEN)
f1.pack(side=LEFT)
localtime=time.asctime(time.localtime(time.time()))
lblInfo=Label(Tops,font=('helvetica',50,'bold'),text="Cafe au Lait ",fg="Black",bd=10,anchor='w')
lblInfo.grid(row=0,column=0)
lblInfo=Label(Tops,font=('arial',20,'bold'),text=localtime,fg="Steel Blue",bd=10,anchor='w')
lblInfo.grid(row=1,column=0)
def Ref():
x=random.randint(10908,500876)
randomRef=str(x)
rand.set(randomRef)
if (Latte.get()==""):
CoL=0
else:
CoL=float(Latte.get())
if (Iced.get()==""):
CoI=0
else:
CoI=float(Iced.get())
if (FlatWhite.get()==""):
CoFW=0
else:
CoFW=float(FlatWhite.get())
if (DoubleExpresso.get()==""):
CoDE=0
else:
CoDE=float(DoubleExpresso.get())
if (Cappacuino.get()==""):
CoC=0
else:
CoC=float(Cappacuino.get())
if (Expresso.get()==""):
CoE=0
else:
CoE=float(Expresso.get())
CostofLatte =CoL * 3.5
CostofIced =CoI * 2.5
CostofFlatWhite =CoFW* 3.75
CostofDoubleExpresso =CoDE * 4.25
CostofCappacuino =CoC* 3.75
CostofExpresso =CoE * 3
CostofMeal= "$", str('%.2f' % (CostofLatte+CostofIced+CostofFlatWhite+CostofDoubleExpresso+CostofCappacuino+CostofExpresso))
TotalCost=(CostofLatte+CostofIced+CostofFlatWhite+CostofDoubleExpresso+CostofCappacuino+CostofExpresso)
OverAllCost ="$", str ('%.2f' % TotalCost)#(PayTax+TotalCost+Ser_Charge))
Cost.set(CostofMeal)
Total.set(OverAllCost)
def Takeaway():
if (Takeaway == 'Yes') or (Takeaway == 'yes') or (Takeaway == 'y') or
(Takeaway == 'Y'):
OverAllCost = (OverAllCost/20 + OverAllCost)
print(OverAllCost)
elif (Takeaway == 'No', 'no', 'n', 'N'):
OverAllCost = (OverAllCost)
def Takeaway():
if Takeaway == 'Yes':
print ('hi')
def qExit():
root.destroy()
def Reset():
rand.set("")
Latte.set("")
FlatWhite.set("")
DoubleExpresso.set("")
Total.set("")
Iced.set("")
Cost.set("")
Cappacuino.set("")
Expresso.set("")
Takeaway.set("")
rand = StringVar()
Latte=StringVar()
DoubleExpresso=StringVar()
Total=StringVar()
Iced=StringVar()
Cost=StringVar()
Cappacuino=StringVar()
Expresso=StringVar()
FlatWhite=StringVar()
lblTakeaway= Label(f1, font=('arial', 16, 'bold'),text="Takeaway",bd=16,anchor="w")
lblTakeaway.grid(row=6, column=0)
txtTakeaway=Entry(f1, font=('arial',16,'bold'),textvariable=Takeaway,bd=10,insertwidth=4,bg="powder blue",justify='right')
txtTakeaway.grid(row=6,column=1)
lblCost= Label(f1, font=('arial', 16, 'bold'),text="Cost of Order",bd=16,anchor="w")
lblCost.grid(row=1, column=2)
txtCost=Entry(f1, font=('arial',16,'bold'),textvariable=Cost,bd=10,insertwidth=4,bg="powder blue",justify='right')
txtCost.grid(row=1,column=3)
lblTotalCost= Label(f1, font=('arial', 16, 'bold'),text="Total Cost",bd=16,anchor="w")
lblTotalCost.grid(row=5, column=2)
txtTotalCost=Entry(f1, font=('arial',16,'bold'),textvariable=Total,bd=10,insertwidth=4,bg="powder blue",justify='right')
txtTotalCost.grid(row=5,column=3)
root.mainloop()
我在将其放入该网站时遇到了一些问题,因此我删除了一些按钮,但是我遗漏了与总数和外卖有关的所有内容。 如果它有帮助,我还没有收到任何错误消息,则根本不会打印。
答案 0 :(得分:0)
您的代码有一些问题。我认为您不了解Tkinter的工作方式。 Tkinter是事件驱动的,全局中的所有if语句仅在程序开始时运行,而不会一遍又一遍地运行mainloop。您需要在此处进行的操作是将所需的所有内容移至ref()
函数中,并在那里管理您的计算。
大多数变量应为IntVar()
而不是StringVar
。
您的许多工作都是多余的,因此我简化了一些代码。也就是说,您的代码可以进一步简化,但我留给您一些思考。
并非所有内容都需要变量名。您所有Label的功能都可以执行,而无需在此处分配变量。
不需要您的Takeaway
函数。我们可以仅在ref
的末尾在输入字段中检查是,然后对总数进行一些额外的数学运算。
获取总数的最简单方法是将Return
绑定到root
并调用ref
。
我还重新编写了您的代码,以更好地适应PEP8编写代码的准则。 像variable_names这样的东西都带有小写和下划线。 You can read more on PEP8 here.
看看下面的代码,如果您有任何疑问,请告诉我。
更新了我的示例以包括其中一种饮料。 根据您的评论中的GitHub链接,这里是固定版本。
import tkinter as tk
import time
root = tk.Tk()
#root.geometry("1600x8000")
root.title("Cafe au Lait")
tops = tk.Frame(root, width=1600, relief="sunken")
tops.pack(side="top")
f1 = tk.Frame(root, width=800, height=700, relief="sunken")
f1.pack(side="left")
localtime = time.asctime(time.localtime(time.time()))
tk.Label(tops, font=('helvetica',50,'bold'), text="Cafe au Lait ", fg="Black", bd=10, anchor='w').grid(row=0, column=0)
tk.Label(tops, font=('arial',20,'bold'), text=localtime, fg="Steel Blue", bd=10, anchor='w').grid(row=1, column=0)
latte = tk.DoubleVar()
double_expresso = tk.DoubleVar()
iced = tk.DoubleVar()
cost = tk.DoubleVar()
cappacuino = tk.DoubleVar()
expresso = tk.DoubleVar()
flat_white = tk.DoubleVar()
total = tk.DoubleVar()
takeaway_var = tk.StringVar()
def ref(even=None):
global total, cost
if latte.get() == "":
col = 0
else:
col = latte.get()
if iced.get() == "":
coi = 0
else:
coi = iced.get()
if flat_white.get() == "":
cofw = 0
else:
cofw = flat_white.get()
if double_expresso.get() == "":
code = 0
else:
code = double_expresso.get()
if cappacuino.get() == "":
coc = 0
else:
coc = cappacuino.get()
if expresso.get() == "":
coe = 0
else:
coe = expresso.get()
costoflatte = col * 3.5
costoficed = coi * 2.5
costofflat_white = cofw * 3.75
costofdouble_expresso = code * 4.25
costofcappacuino = coc * 3.75
costofexpresso = coe * 3
total.set('{}'.format(cost.get() + costoflatte + costoficed + costofflat_white + costofdouble_expresso + costofcappacuino + costofexpresso))
print('{}'.format(cost.get() + costoflatte + costoficed + costofflat_white + costofdouble_expresso + costofcappacuino + costofexpresso))
if txt_takeaway.get() in ['Yes', 'yes', 'y', 'Y']:
x = total.get()
y = x * 0.05
print(x, y)
total.set(y + x)
def q_exit():
root.destroy()
def reset():
global latte, flat_white, double_expresso, total, iced, cost, cappacuino, expresso, takeaway_var
latte = tk.DoubleVar()
double_expresso = tk.DoubleVar()
iced = tk.DoubleVar()
cost = tk.DoubleVar()
cappacuino = tk.DoubleVar()
expresso = tk.DoubleVar()
flat_white = tk.DoubleVar()
total = tk.DoubleVar()
takeaway_var = tk.StringVar()
tk.Label(f1, font=('arial', 16, 'bold'), text="Takeaway", bd=16, anchor="w").grid(row=6, column=0)
txt_takeaway = tk.Entry(f1, font=('arial',16,'bold'),textvariable=takeaway_var, bd=10, insertwidth=4, bg="powder blue", justify='right')
txt_takeaway.grid(row=6, column=1)
tk.Label(f1, font=('arial', 16, 'bold'), text="cost of Order", bd=16, anchor="w").grid(row=1, column=2)
txt_cost = tk.Entry(f1, font=('arial',16,'bold'), textvariable=cost,bd=10, insertwidth=4, bg="powder blue", justify='right')
txt_cost.grid(row=1, column=3)
tk.Label(f1, font=('arial', 16, 'bold'), text="total cost", bd=16, anchor="w").grid(row=5, column=2)
txt_totalcost = tk.Entry(f1, font=('arial',16,'bold'), textvariable=total, bd=10, insertwidth=4, bg="powder blue", justify='right')
txt_totalcost.grid(row=5, column=3)
tk.Label(f1, font=('arial', 16, 'bold'),text="Latte",bd=16,anchor="w").grid(row=1, column=0)
txt_latte=tk.Entry(f1, font=('arial',16,'bold'),textvariable=latte,bd=10,insertwidth=4,bg="powder blue",justify='right')
txt_latte.grid(row=1,column=1)
tk.Label(f1, font=('arial', 16, 'bold'),text="Flat White",bd=16,anchor="w").grid(row=2, column=0)
txt_flat_white=tk.Entry(f1, font=('arial',16,'bold'),textvariable=flat_white,bd=10,insertwidth=4,bg="powder blue",justify='right')
txt_flat_white.grid(row=2,column=1)
tk.Label(f1, font=('arial', 16, 'bold'),text="Double Expresso",bd=16,anchor="w").grid(row=3, column=0)
txt_double_expresso=tk.Entry(f1, font=('arial',16,'bold'),textvariable=double_expresso,bd=10,insertwidth=4,bg="powder blue",justify='right')
txt_double_expresso.grid(row=3,column=1)
tk.Label(f1, font=('arial', 16, 'bold'),text="Cappacuino",bd=16,anchor="w").grid(row=4, column=0)
txt_cappacuino=tk.Entry(f1, font=('arial',16,'bold'),textvariable=cappacuino,bd=10,insertwidth=4,bg="powder blue",justify='right')
txt_cappacuino.grid(row=4,column=1)
tk.Label(f1, font=('arial', 16, 'bold'),text="Expresso",bd=16,anchor="w").grid(row=5, column=0)
txt_expresso=tk.Entry(f1, font=('arial',16,'bold'),textvariable=expresso,bd=10,insertwidth=4,bg="powder blue",justify='right')
txt_expresso.grid(row=5,column=1)
tk.Label(f1, font=('arial', 16, 'bold'),text="Iced",bd=16,anchor="w").grid(row=0, column=0)
txt_iced=tk.Entry(f1, font=('arial',16,'bold'),textvariable=iced,bd=10,insertwidth=4,bg="powder blue",justify='right')
txt_iced.grid(row=0,column=1)
btnTotal=tk.Button(f1,padx=16,pady=8,bd=16,fg="black",font=('arial',16,'bold'),width=10,text="Total",bg="powder blue",command=ref).grid(row=7,column=1)
root.bind("<Return>", ref)
root.mainloop()