首页 > 编程问答 >通过删除按钮从列表框中删除时,减去数字值时遇到一些问题

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

时间:2024-08-09 03:56:43浏览次数:15  
标签:python tkinter listbox

我尝试了不同的组合来尝试在点餐时获取要减去的值,并成功地将项目从列表框中删除,但尚未从“总价值”中删除项目值。这是我多次尝试获取要减去的值时遇到的问题。有谁能够获得从列表框中删除后从总值中减去的值吗?

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))

说明:

  1. 获取已删除项目的索引: 在从 final_order 中删除项目之前,我们使用 .get() 方法获取列表框中选定项目的文本,然后使用 .index() 方法在 final_order 列表中找到该项目的索引。

  2. final_order_price 中删除: 我们使用在 final_order 中找到的相同索引来从 final_order_price 列表中删除相应的项目价格。

  3. 更新总价: 我们更新 new_price 变量,从其当前值中减去已删除项目的价格。

  4. 更新总价标签: 最后,我们使用更新后的 new_price 值重新配置 total_cost_label

通过这些更改,现在从列表框中删除项目应该可以正确更新总价。

其他建议:

  • 考虑使用面向对象的方法来更好地组织的代码。这将使的代码更易于管理和扩展。可以创建一个 OrderItem 类来存储有关每个订单项(名称和价格)的信息,并使用这些对象的列表而不是单独的名称和价格列表。
  • 使用 try-except 块来处理可能出现的错误,例如,当列表框为空且用户尝试删除项目时。

希望这些改进对有所帮助!如果有任何其他问题,请告诉我。

标签:python,tkinter,listbox
From: 78848385

相关文章

  • 如何从我的 Python 应用程序更新我的 Facebook Business 令牌?
    我有一个使用FacebookBusiness库的Python应用程序。因此,我需要使用Facebook提供的令牌来访问我的见解并操纵它们。但是,这个令牌有一个很长的到期日期,但我想知道是否有办法自动更新这个令牌在我的应用程序中,这样它就不会停止运行。当然可以!你可以使用Facebook提......
  • 哪种编程语言更适合学习数据结构和算法:C++、Java 还是 Python?
    作为一名工程专业的学生,​​我正在尝试决定使用哪种编程语言来学习数据结构和算法(DSA)。我正在考虑C++,它提供高性能和强大的标准模板库,但对于初学者来说可能很复杂。Java具有强大的语法和内置集合,使DSA概念更容易掌握,尽管我不确定它与C++相比的性能。Python以其简单性和......
  • 同时运行多个异步阻塞函数 - Python
    我是Python和协程的新手,我正在尝试利用Python的asyncio库来并行处理阻塞函数。我正在使用python3.8.6。我有一个阻塞函数,它从数组输入中接收不同的输入,我需要每个输入的阻塞函数同时运行。我已经尝试过,但它们似乎仍然按顺序运行:asyncdefmain():tasks=[asyncio......
  • 使用两个连接的字符串调用变量 Python
    抱歉缺乏细节,因为我是python的初学者:c1=c2=c3=c4=c5=Falsex=int(input("Enteranumber1-5:"))ifx>5orx<1:print("Yournumbermustbebetween1and5")else:"c",x=True第8行是连接2个字符串的地方。我不确定......
  • 测试Python中是否存在可执行文件?
    在Python中,有没有一种可移植且简单的方法来测试可执行程序是否存在?简单我的意思是类似which命令的东西,这将是完美的。我不想手动搜索PATH或涉及尝试使用Popen&al执行它并查看它是否失败(这就是我现在正在做的事情,但想象它是launchmissiles)......
  • Python 和 Excel:将数据放入另一个函数中,然后从中获取信息
    我正在尝试将温度/压力数据放入蒸汽表以获得过热焓数据。我已经成功地获取了数据并将其放入Excel文件中,然后它为我提取了焓数据。问题是,当我将温度和压力数据放入蒸汽表时,它实际上并没有进行双重插值,因此焓(H)值实际上从未改变ng。我最终只得到了蒸汽数据中给出......
  • Python 类型提示:显式排除无效的重载组合?
    我有一个带有两个参数的函数,每个参数都可以采用两种类型之一。四个成对组合中的三个有效,但第四个无效。我想找到一种方法来键入提示此场景,可以检查这些类型,但不一定每次调用foo()时都必须编写代码来检查无效情况。有没有办法可以改进foo()、bar()或两......
  • 我在制作 python 语音应用程序时遇到错误
    我编写了一个语音聊天应用程序代码,但是当我们运行此代码并加入语音频道时,我收到照片中的错误错误1错误2这是我的代码;客户端代码:importtkinterastkfromtkinterimportmessageboximportpyaudioimportsocketimportthreadingimporttimeHOST=......
  • Tkinter 按钮不更新变量
    我有一个类中有两个单选按钮的GUI,使用tkinter的第一个单选按钮旨在将变量save_to_excel设置为True,而第二个单选按钮应为false,这是为了让用户确定是否要保存信息作为Excel工作表或文本文档。我有一个旧版本的代码,可以工作并正确更新变量,但是新版本必须更改某些内容,而新版......
  • pyocr,一个超酷的Python库!
    pyocr是一个用于光学字符识别(OCR)的Python库,它提供了一个简单的接口,允许开发者将图片中的文本提取出来。这个库是对Tesseract-OCR的封装,使得在Python环境中使用OCR技术变得更加便捷。如何安装pyocr首先,要使用pyocr库,您需要安装它。可以使用pip包管理工具来进......