通过删除按钮从列表框中删除时,减去数值时遇到一些问题

问题描述 投票:0回答:1

我尝试了不同的组合来尝试在点餐时获取要减去的值,并成功地将项目从列表框中删除,但尚未从“总价值”中删除项目值,这就是我多次尝试获取要减去的值时遇到的问题....一旦从列表框中删除,任何人都能够从总值中减去要减去的值吗???请并谢谢!

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```
python tkinter listbox
1个回答
1
投票

一开始,你就搞错了。

Stackoverflow 不是代码编写服务。你不能只发布 422 行代码并说“嘿,我想要这个按钮做到这一点,我该怎么做?”。 抱歉,但是您在问题上投入了零努力,因此您不能真正期望人们投入精力来回答它。由于您是新手,也许可以看看提问指南。

这里有一些你可以做得更好的事情:

  • 不要只是复制和粘贴整个代码。发布对您的问题很重要的相关函数/片段,例如这里的删除函数。您可以在最后添加完整的代码,以便其他人可以运行您的程序,但不要仅发布该代码并期望其他人找到重要的内容。

  • 解释你的代码是如何工作的。

    你甚至没有写关于你的代码的功能的介绍,你只是从“我尝试了不同的组合......”开始组合什么?准确一点。并且还解释了一些可读的。你把整个问题压缩成一个大句子,这很难理解。

  • 说出您尝试过的内容。

    您说过您尝试过不同的组合,但具体是什么?为什么它不起作用?您期待什么以及您得到什么?

  • 我知道这样做很烦人,但是如果您遵循指导方针写出好问题,人们可以更好地帮助您。

对于您的问题:由于 ListBox 中的条目的放置顺序与变量

final_order

final_order_price
中的项目的顺序相同,因此您始终可以假设例如列表框中的第二个条目将引用
final_order
final_order_price
中的第二项。
因此,您可以使用所选列表框条目的索引并从两个列表中弹出该索引,然后更新价格标签。

对我来说,它是这样工作的:

def remove_item(): selected_checkbox = Lb.curselection()[0] Lb.delete(selected_checkbox) final_order.pop(selected_checkbox) final_order_price.pop(selected_checkbox) new_price = (sum(final_order_price)) total_cost_label.configure(text=f'total cost is: ${new_price} ',font=("Comic Sans MS", 12))

但是说真的,你应该尝试重写你的代码。

你了解OOP吗?您可以将它们包装在一个类中,例如,而不是一遍又一遍地声明图像、按钮、菜单等。使用 for 循环重复将小部件放置在窗口中。你可以做很多事情。如果这对你来说太复杂,也许首先尝试一些较小的项目,不需要写那么多,观看教程等......我不知道你已经有多少经验了。

最后一件事:当我运行代码时,你的窗口有很多错误,我需要点击元素大约 10 次才能做出反应。我不知道这对你来说是否相同,但你应该重新尝试重写/重构你的代码并使其更加干净。

希望我能帮助你并且你学到了一些东西!

© www.soinside.com 2019 - 2024. All rights reserved.