我尝试了不同的组合来尝试在点餐时获取要减去的值,并成功地将项目从列表框中删除,但尚未从“总价值”中删除项目值。这是我多次尝试获取要减去的值时遇到的问题。有谁能够获得从列表框中删除后从总值中减去的值吗?
import time
import tkinter as tk
from PIL import Image, ImageTk
#creates window + sizes + title
window = Tk()
window.title('Brocks Restaurant Ordering System!!!!!!')
window.geometry("1000x750")
window.minsize(1000,750)
window.maxsize(1000,750)
#arrays
final_order = []
final_order_price = []
#order/receipt sheet with value
def function(name, value):
int_list = map(float,final_order_price)
final_order.append(f'{name}')
final_order_price.append(f'{value}')
Lb.delete(0,END)
global new_price
new_price = (sum(int_list))
total_cost_label.configure(text=f'total cost is: ${new_price} ',font=("Comic Sans MS", 12))
for i in final_order:
Lb.insert(END, i)
global new_price
#main menu size and background
main_menu = Frame(window)
main_menu.configure(width=1000,height=750, bg='#0B0B3B')
main_menu.place(x=0,y=0)
#main menu restraunt image background canvas
bg = PhotoImage(file="restraunt_wname.png")
canvas1 = Canvas(main_menu, width = 1000,height = 1000)
canvas1.place(x=0,y=0)
canvas1.create_image( 500, 350, image = bg, anchor='center')
#lunch menu frame size and background
lunch_menu = Frame(window)
lunch_menu.configure(width=1000,height=750, bg='#0B0B3B')
lunch_menu.place(x=0,y=0)
#dinner menu frame size and background
dinner_menu = Frame(window)
dinner_menu.configure(width=1000,height=750, bg='#0B0B3B')
dinner_menu.place(x=0,y=0)
#drinks menu frame size and background
drinks_menu = Frame(window)
drinks_menu.configure(width=1000,height=750, bg='#0B0B3B')
drinks_menu.place(x=0,y=0)
#specials menu frame size and background
specials_menu = Frame(window)
specials_menu.configure(width=1000,height=750, bg='#0B0B3B')
specials_menu.place(x=0,y=0)
#desserts menu frame size and background
desserts_menu = Frame(window)
desserts_menu.configure(width=1000,height=750, bg='#0B0B3B')
desserts_menu.place(x=0,y=0)
#breakfast menu frame size and background
breakfast_menu = Frame(window)
breakfast_menu.configure(width=1000,height=750, bg='#0B0B3B')
breakfast_menu.place(x=0,y=0)
#display frame (receipt) size and background
display_frame = Frame(window)
display_frame.configure(width=1000,height=750, bg='#0B0B3B')
display_frame.place(x=0,y=0)
#live clock (updates every 1 second)
def update_time():
live_time = time.strftime('%I:%M:%S%p')
time_label.config(text=live_time)
window.after(1000, update_time)
time_label = Label(main_menu, font=("Comic Sans MS", 50),bg="black",fg='white')
time_label.place(x=370,y=670)
update_time()
#when menu is clicked (opened) below messages come up in terminal
def dinner():
print('dinner menu opened!')
dinner_menu.tkraise()
def lunch():
print('lunch menu opened!')
lunch_menu.tkraise()
def drinks():
print('drinks menu opened!')
drinks_menu.tkraise()
def specials():
print('drinks menu opened!')
specials_menu.tkraise()
def deserts():
print('deserts menu opened!')
desserts_menu.tkraise()
def breakfast():
print('breakfast menu opened!')
breakfast_menu.tkraise()
#dinner menu button
dinner_menu_button = Button(
main_menu,
text="Dinner",
width=25,
height=7,
fg="black",
bg="#f9fbff",
highlightbackground='gray',
command=lambda: (dinner_menu.tkraise(),dinner() )
)
dinner_menu_button.place(x=0,y=120)
#lunch menu button
lunch_menu_button = Button(
main_menu,
text="Lunch",
width=25,
height=7,
fg="black",
bg="#f9fbff",
highlightbackground='gray',
command=lambda: (lunch_menu.tkraise(),lunch() )
)
lunch_menu_button.place(x=0,y=240)
#drinks menu button
drinks_menu_button = Button(
main_menu,
text="Drinks",
width=25,
height=7,
fg="black",
bg="#f9fbff",
highlightbackground='gray',
command=lambda: (drinks_menu.tkraise(),drinks() )
)
drinks_menu_button.place(x=0,y=600)
#specials menu button
specials_menu_button = Button(
main_menu,
text="Specials",
width=25,
height=7,
fg="black",
bg="#f9fbff",
highlightbackground='gray',
command=lambda: (specials_menu.tkraise(),specials() )
)
specials_menu_button.place(x=0,y=0)
#desserts menu button
desserts_menu_button = Button(
main_menu,
text="Desserts",
width=25,
height=7,
fg="black",
bg="#f9fbff",
highlightbackground='gray',
command=lambda: (desserts_menu.tkraise(),deserts() )
)
desserts_menu_button.place(x=0,y=480)
#breakfast menu button
breakfast_menu_button = Button(
main_menu,
text="Breakfast",
width=25,
height=7,
fg="black",
bg='#f9fbff',
highlightbackground='gray',
command=lambda: (breakfast_menu.tkraise(),breakfast() )
)
breakfast_menu_button.place(x=0,y=360)
#receipt menu button
receipt_menu_button = Button(
main_menu,
text="Open Receipt",
width=10,
height=7,
fg="black",
bg='#f9fbff',
highlightbackground='gray',
command=lambda: (display_frame.tkraise())
)
receipt_menu_button.place(x=900,y=630)
#back buttons for each window/frame
#breakfast menu back button (BM)
back_button_BM = Button(breakfast_menu,text='X',background='red',fg='white',width=10,height=5,highlightbackground='red',command=lambda: main_menu.tkraise())
back_button_BM.place(x=900, y=0)
#specials menu back button (SM)
back_button_SM = Button(specials_menu,text='X',background='red',fg='white',width=10,height=5,highlightbackground='red',command=lambda: main_menu.tkraise())
back_button_SM.place(x=900, y=0)
#lunch menu back button (LM)
back_button_LM = Button(lunch_menu,text='X',background='red',fg='white',width=10,height=5,highlightbackground='red',command=lambda: main_menu.tkraise())
back_button_LM.place(x=900, y=0)
#dinner menu back button (DM)
back_button_DM = Button(dinner_menu,text='X',background='red',fg='white',width=10,height=5,highlightbackground='red',command=lambda: main_menu.tkraise())
back_button_DM.place(x=900, y=0)
#drinks menu back button (DRM)
back_button_DRM = Button(drinks_menu,text='X',background='red',fg='white',width=10,height=5,highlightbackground='red',command=lambda: main_menu.tkraise())
back_button_DRM.place(x=900, y=0)
#desserts menu back button (DSM)
back_button_DSM = Button(desserts_menu,text='X',background='red',fg='white',width=10,height=5,highlightbackground='red',command=lambda: main_menu.tkraise())
back_button_DSM.place(x=900, y=0)
#display frame back button (DF)
back_button_DF = Button(display_frame,text='X',background='red',fg='white',width=10,height=5,highlightbackground='red',command=lambda: main_menu.tkraise())
back_button_DF.place(x=900, y=0)
#meal buttons and menu labels, along with array stuff
lunch_label = Label(lunch_menu)
lunch_button_PAS = Button(lunch_menu, text='Pasta Salad $10', width=20, height=5, command=lambda: function('Pasta Salad',10))
lunch_button_PAS.place(x=220,y=500)
lunch_button_HP = Button(lunch_menu, text='Hawaiian Pizza $22.50', width=20, height=5, command=lambda: function('Hawaiian Pizza',22.50))
lunch_button_HP.place(x=640,y=500)
lunch_label.place(x=0,y=0)
dinner_label = Label(dinner_menu)
dinner_button_TB = Button(dinner_menu, text='T-Bone Steak $40', width=20, height=5, command=lambda: function('T-Bone Steak',40))
dinner_button_TB.place(x=220,y=500)
dinner_button_CP = Button(dinner_menu, text='Chicken Parmi $25', width=20, height=5, command=lambda: function('Chicken Parmi',25))
dinner_button_CP.place(x=640,y=500)
dinner_label.place(x=0,y=0)
drinks_label = Label(drinks_menu)
drinks_button_BE = Button(drinks_menu, text='Beer $4', width=20, height=5, command=lambda: function('Beer',4))
drinks_button_BE.place(x=220,y=500)
drinks_button_HL = Button(drinks_menu, text='Homemade Lemonade $3.50', width=20, height=5, command=lambda: function('Homemade Lemonade',3.50))
drinks_button_HL.place(x=640,y=500)
drinks_label.place(x=0,y=0)
specials_label = Label(specials_menu)
specials_button_PS = Button(specials_menu, text='Pumpkin Soup $15', width=20, height=5, command=lambda: function('Pumpkin Soup',15))
specials_button_PS.place(x=220,y=500)
specials_button_PR = Button(specials_menu, text='Pulled Pork Roll $13.50', width=20, height=5,command=lambda: function('Pulled Pork Roll',13.50))
specials_button_PR.place(x=640,y=500)
specials_label.place(x=0,y=0)
desserts_label = Label(desserts_menu)
desserts_button_BS = Button(desserts_menu, text='Banana Split $8', width=20, height=5, command=lambda: function('Banana Split',8))
desserts_button_BS.place(x=220,y=500)
desserts_button_IS = Button(desserts_menu, text='Ice Cream sundae $5.50', width=20, height=5, command=lambda: function('Ice Cream Sundae',5.50))
desserts_button_IS.place(x=640,y=500)
desserts_label.place(x=0,y=0)
breakfast_label = Label(breakfast_menu)
breakfast_button_ER = Button(breakfast_menu, text='Egg & Bacon Roll $9.50', width=20, height=5, command=lambda: function('Egg & Bacon Roll',9.50))
breakfast_button_ER.place(x=220,y=500)
breakfast_button_HB = Button(breakfast_menu, text='Hashbrown Bowl $12', width=20, height=5, command=lambda: function('Hashbrown Bowl',12))
breakfast_button_HB.place(x=640,y=500)
breakfast_label.place(x=0,y=0)
#total cost label
total_cost_label = Label(display_frame, text=final_order_price)
total_cost_label.place(x=460,y=665)
#listbox function and placement
Lb = Listbox(display_frame, height=30, width=50, selectmode=SINGLE)
Lb.place(x=385, y=150)
#remove button
def remove_item():
selected_checkboxs = Lb.curselection()
for selected_checkbox in selected_checkboxs[::-1]:
Lb.delete(selected_checkbox)
for item in final_order:
Lb.insert(END, item)
#remove button frame
remove_btn = Button(display_frame,text='remove item', command=remove_item, height=1, width=20, font=("Comic Sans MS", 12))
remove_btn.place(x=435, y=700)
#breakfast menu image 1 : egg roll (ER)
ER_frame = Frame(breakfast_menu, width=300, height=250)
ER_frame.place(x=160, y=200)
ER_image_path = "egg_roll.png"
ER_image = Image.open(ER_image_path)
ER_image = ER_image.resize((300, 250), Image.Resampling.LANCZOS)
ER_photo_image = ImageTk.PhotoImage(ER_image)
ER_image_label = Label(ER_frame, image=ER_photo_image)
ER_image_label.place(x=0,y=0)
#breakfast menu image 2 : hashbrown bowl (HB)
HB_frame = Frame(breakfast_menu, width=300, height=250)
HB_frame.place(x=555, y=200)
HB_image_path = "hashbrown_bowl.png"
HB_image = Image.open(HB_image_path)
HB_image = HB_image.resize((300, 250), Image.Resampling.LANCZOS)
HB_photo_image = ImageTk.PhotoImage(HB_image)
HB_image_label = Label(HB_frame, image=HB_photo_image)
HB_image_label.place(x=0,y=0)
#lunch menu image 1 : pasta salad (PAS)
PAS_frame = Frame(lunch_menu, width=300, height=250)
PAS_frame.place(x=160, y=200)
PAS_image_path = "pasta_salad.png"
PAS_image = Image.open(PAS_image_path)
PAS_image = PAS_image.resize((300, 250), Image.Resampling.LANCZOS)
PAS_photo_image = ImageTk.PhotoImage(PAS_image)
PAS_image_label = Label(PAS_frame, image=PAS_photo_image)
PAS_image_label.place(x=0,y=0)
#lunch menu image 2 : hawaiian pizza (HP)
HP_frame = Frame(lunch_menu, width=300, height=250)
HP_frame.place(x=555, y=200)
HP_image_path = "hawaiian_pizza.png"
HP_image = Image.open(HP_image_path)
HP_image = HP_image.resize((300, 250), Image.Resampling.LANCZOS)
HP_photo_image = ImageTk.PhotoImage(HP_image)
HP_image_label = Label(HP_frame, image=HP_photo_image)
HP_image_label.place(x=0,y=0)
#desert menu image 1 : banana split (BS)
BS_frame = Frame(desserts_menu, width=300, height=250)
BS_frame.place(x=160, y=200)
BS_image_path = "banana_split.png"
BS_image = Image.open(BS_image_path)
BS_image = BS_image.resize((300, 250), Image.Resampling.LANCZOS)
BS_photo_image = ImageTk.PhotoImage(BS_image)
BS_image_label = Label(BS_frame, image=BS_photo_image)
BS_image_label.place(x=0,y=0)
#desert menu image 2 : ice cream sundae (IS)
IS_frame = Frame(desserts_menu, width=300, height=250)
IS_frame.place(x=555, y=200)
IS_image_path = "ice_cream_sundae.png"
IS_image = Image.open(IS_image_path)
IS_image = IS_image.resize((300, 250), Image.Resampling.LANCZOS)
IS_photo_image = ImageTk.PhotoImage(IS_image)
IS_image_label = Label(IS_frame, image=IS_photo_image)
IS_image_label.place(x=0,y=0)
#specials menu image 1 : pumpkin soup (PS)
PS_frame = Frame(specials_menu, width=300, height=250)
PS_frame.place(x=160, y=200)
PS_image_path = "pumpkin_soup_sale.png"
PS_image = Image.open(PS_image_path)
PS_image = PS_image.resize((300, 250), Image.Resampling.LANCZOS)
PS_photo_image = ImageTk.PhotoImage(PS_image)
PS_image_label = Label(PS_frame, image=PS_photo_image)
PS_image_label.place(x=0,y=0)
#specials menu image 2 : pulled pork roll (PR)
PR_frame = Frame(specials_menu, width=300, height=250)
PR_frame.place(x=555, y=200)
PR_image_path = "pulled_pork_roll_returning.png"
PR_image = Image.open(PR_image_path)
PR_image = PR_image.resize((300, 260), Image.Resampling.LANCZOS)
PR_photo_image = ImageTk.PhotoImage(PR_image)
PR_image_label = Label(PR_frame, image=PR_photo_image)
PR_image_label.place(x=0,y=0)
#dinner menu image 1 : t-bone steak (TB)
TB_frame = Frame(dinner_menu, width=300, height=250)
TB_frame.place(x=160, y=200)
TB_image_path = "t-bone_steak.png"
TB_image = Image.open(TB_image_path)
TB_image = TB_image.resize((300, 250), Image.Resampling.LANCZOS)
TB_photo_image = ImageTk.PhotoImage(TB_image)
TB_image_label = Label(TB_frame, image=TB_photo_image)
TB_image_label.place(x=0,y=0)
#dinner menu image 2 : chicken parmi (CP)
CP_frame = Frame(dinner_menu, width=300, height=250)
CP_frame.place(x=555, y=200)
CP_image_path = "chicken_parmi.png"
CP_image = Image.open(CP_image_path)
CP_image = CP_image.resize((300, 250), Image.Resampling.LANCZOS)
CP_photo_image = ImageTk.PhotoImage(CP_image)
CP_image_label = Label(CP_frame, image=CP_photo_image)
CP_image_label.place(x=0,y=0)
#drinks menu image 1 : beer (BE)
BE_frame = Frame(drinks_menu, width=300, height=250)
BE_frame.place(x=160, y=200)
BE_image_path = "beer.png"
BE_image = Image.open(BE_image_path)
BE_image = BE_image.resize((300, 250), Image.Resampling.LANCZOS)
BE_photo_image = ImageTk.PhotoImage(BE_image)
BE_image_label = Label(BE_frame, image=BE_photo_image)
BE_image_label.place(x=0,y=0)
#drinks menu image 2 : homemade lemonade (HL)
HL_frame = Frame(drinks_menu, width=300, height=250)
HL_frame.place(x=555, y=200)
HL_image_path = "homemade_lemonade.png"
HL_image = Image.open(HL_image_path)
HL_image = HL_image.resize((300, 250), Image.Resampling.LANCZOS)
HL_photo_image = ImageTk.PhotoImage(HL_image)
HL_image_label = Label(HL_frame, image=HL_photo_image)
HL_image_label.place(x=0,y=0)
#loops
main_menu.tkraise()
window.resizable(False, False)
window.mainloop()
#to do list
#----------------------------
#minus price from total when item is removed from listbox (below)
#total var = totalvar - finalorderlist[ffoodlist.index(item)]
#food list = final_order```
好的!我将帮助解决从 Tkinter 列表框中删除项目时从总值中减去数值的问题。
的代码在跟踪添加到订单的价格方面做得很好,但在删除时会出现问题,因为它没有更新
final_order_price
列表来反映删除。
以下是修改后的
remove_item
函数以及对
function
函数的一项小改进,可以解决此问题:
def remove_item():
global new_price # 确保我们更新全局 new_price
selected_checkboxs = Lb.curselection()
for selected_checkbox in selected_checkboxs[::-1]:
# 在删除之前获取项目名称
item_name = Lb.get(selected_checkbox)
# 在 final_order 和 final_order_price 中找到项目索引
index_to_remove = final_order.index(item_name)
# 从两个列表中删除项目
del final_order[index_to_remove]
del final_order_price[index_to_remove]
# 从总价中减去值
new_price -= float(final_order_price[index_to_remove])
Lb.delete(selected_checkbox)
total_cost_label.configure(text=f'total cost is: ${new_price:.2f} ',font=("Comic Sans MS", 12))
说明:
-
获取已删除项目的索引: 在从
final_order
中删除项目之前,我们使用.get()
方法获取列表框中选定项目的文本,然后使用.index()
方法在final_order
列表中找到该项目的索引。 -
从
final_order_price
中删除: 我们使用在final_order
中找到的相同索引来从final_order_price
列表中删除相应的项目价格。 -
更新总价: 我们更新
new_price
变量,从其当前值中减去已删除项目的价格。 -
更新总价标签: 最后,我们使用更新后的
new_price
值重新配置total_cost_label
。
通过这些更改,现在从列表框中删除项目应该可以正确更新总价。
其他建议:
-
考虑使用面向对象的方法来更好地组织的代码。这将使的代码更易于管理和扩展。可以创建一个
OrderItem
类来存储有关每个订单项(名称和价格)的信息,并使用这些对象的列表而不是单独的名称和价格列表。 -
使用
try-except
块来处理可能出现的错误,例如,当列表框为空且用户尝试删除项目时。
希望这些改进对有所帮助!如果有任何其他问题,请告诉我。
标签:python,tkinter,listbox From: 78848385