python - 我非常坚持如何添加外卖选项,因为定义不起作用
问题描述
在我的代码中,我通过添加我想购买的饮料数量来让它工作,但是当尝试添加代码时,如果他们在外卖输入框中输入“是”,它将在总数中增加 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()
我在把它放到这个网站上时遇到了一些问题,所以我拿走了一些按钮,但我留下了与总数和我遇到问题的外卖有关的所有内容。如果它有帮助,我没有收到任何错误消息,它根本不打印。
解决方案
您的代码有一些问题。我认为您不了解 Tkinter 的工作原理。Tkinter 是事件驱动的,全局中的所有 if 语句仅在程序开始时运行,而不是在主循环中一遍又一遍地运行。您需要在这里做的是将您需要的所有内容移入ref()
函数并在那里管理您的计算。
您的大多数变量应该是IntVar()
而不是StringVar
.
你的很多作业都是多余的,所以我简化了你的一些代码。那就是说您的代码可以进一步简化,但我留下了一些让您思考。
不是所有东西都需要一个变量名。您的所有标签都可以在此处不分配给变量的情况下执行。
Takeaway
不需要您的功能。我们可以在ref
输入字段的最后检查是否是,然后对总数进行一些额外的数学运算。
获取总数的最简单方法是绑定Return
并root
调用ref
.
我还重新编写了您的代码,以更好地适应编写代码的 PEP8 指南。变量名之类的东西都是小写和下划线。您可以在此处阅读有关 PEP8 的更多信息。
看看下面的代码,如果您有任何问题,请告诉我。
更新了我的示例以包括其中一种饮料。根据你这里评论中的 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()
推荐阅读
- blazor - Blazor 网格绑定列按字段名称
- python-3.x - 以列表为列将 CSV 导入 pandas 数据框
- google-apps-script - 如何在 Google 脚本中上传文件到驱动器
- javascript - 构建后,我们得到 argon-dashboard-react/static/js/main.e8eb0f76.chunk.js net::ERR_ABORTED 404 (Not Found)
- expo - 如何关闭世博航站楼
- javascript - 两个 int 值之间的按位或运算后的负值
- vega-lite - 有没有办法在 vega-lite 中动态检测文本?
- javascript - this.setState 不改变状态值
- powershell - 将项目从/移到 unc 路径长度限制?
- jenkins - 在Jenkins中执行wget时如何通配文件名