微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Python Tkinter 菜单系统无法汇总和显示价格

如何解决Python Tkinter 菜单系统无法汇总和显示价格

python 新手,我一直在尝试用 Tkinter 构建一个菜单,我的想法是当我从菜单中选择一个项目时,它的名称出现在较大的屏幕中,而所选项目的总数出现在较小的屏幕中屏幕,我的函数叫做 fireFood。我目前看到我的价格在一条线上运行,而不是被总计,我已经坚持了几天,希望有人能指出我正确的方向。

rom tkinter import ttk
import tkinter as tk
root = tk.Tk()
root.geometry('500x300')
root.title('Server Window')
root.wm_resizable(width=False,height=False)

# Create display area for selected items

display = tk.Text(root,height=10,width=30,bg='Orange',bd=4)
display.grid(row=1,column=3)

price_display = tk.Text(root,height=3,width=15,bd=4)
price_display.grid(row=3,column=3)
#====================== Functions =================================
def fireFood():
    # Every time a new item is selected i want a new total to be calculated and displayed
    global menu
    global price_display
    global display
    global select_option
    total = 0
    prices = []
    # this inserts food item onto display
    display.insert(tk.END,options.get())
    prices.append([options.get(),menu[options.get()]])

    for x in prices:
        total = total + float(x[1])

    # this shows price on lower display
        price_display.insert(tk.END,total)

    total += float(menu[options.get()])

def addList(arr):
    cost = 0
    arr.remove('\n')
    total = [sum(float(x) for x in arr)]
    for x in total:
        cost += x
    return cost
#======================================================================

# Create a Dictionary of items to be displayed in ComboBox
menu = {'fries ':5,'Burger ':10,'Pizza ':15,'Chicken ':8,'Fish ':7.50}
menu_list = [x for x in menu.keys()]
menu_prices = [y for y in menu.values()]
options = ttk.ComboBox(values=menu_list)

# Set default value for the comboBox to 'Menu' to function as a pseudo label
options.set('Menu')
options.grid(row=0,column=0)

# Create a button that when pressed will get the value of comboBox
select_option = ttk.Button(root,text='Select',command=fireFood)
select_option.grid(row=0,column=1)

root.mainloop()

解决方法

这对我有用。

price_display = tk.Text(root,height=3,width=15,bg='Orange',bd=4)
price_display.grid(row=3,column=3)

total = 0

#====================== Functions =================================
def fireFood():
    # Every time a new item is selected i want a new total to be calculated and displayed
    global menu
    global price_display
    global display
    global select_option
    global prices
    global total # make total global
    prices = []
    # this inserts food item onto display
    display.insert(tk.END,options.get())

    # this shows price on lower display
    total += float(menu[options.get()])
    price_display.delete('1.0','end') # delete previous text
    price_display.insert(tk.END,total)

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。