如何从字典中总结出总数

2024-04-27 02:50:24 发布

您现在位置:Python中文网/ 问答频道 /正文

我想做一个和在商店接受订单有关的程序。整个代码都是关于执行订单然后创建标签列表的页面。一切都做得很好,但我不能得到总的价格。这是我的密码:

def blnclick():
    n=0
    uprice = price.get()
    uitem = item.get()
    order = {'items': uitem, 'price': uprice}
    lab = order['items']
    lab1 = order['price']
    total = int(lab1)
    c = Canvas(canvas_frme, bg='white', bd=10, width=10, height=50)
    c.pack()
    for values in order:
        Label(c, text=lab, bg='white', font=('candara', 19)).grid(row=n, column=0)
        Label(c, text=lab1, bg='white', font=('candara', 19)).grid(row=n, column=1)
        totSum.set(total)
        total = total+total

提前谢谢你的帮助。你知道吗

更新:我认为代码不擅长处理字典,因为每次我尝试一个新代码来合计价格时,它总是将输入的didget相加一次,并在插入新条目后重置。什么都行。 请注意,我仍然是python的学习者/学生,因此代码中的任何工作更改都是可以接受的。你知道吗

更新#2当你们去购物时,你们会购买多个商品,然后商品列表会显示出总金额。我正努力做到这一点。请随意更改代码,根据您的需要,因为我是一个学习者,我犯了很多次错误。谢谢大家


Tags: 代码订单列表getlaborderitems价格
3条回答

使用sum非常简单:

total = sum(int(value) for key, value in order.items())

当前创建的局部变量total不能保留旧值。
除此之外,还可以指定新值total = int(lab1),这样就可以删除旧值。你知道吗

您需要全局变量(global total)来始终保持总值。
然后您可以向total添加新的值,这将保留旧的值。你知道吗

要将小部件添加到画布,必须使用canvas_window((x,y), widget)
我们使用pack()/grid()将小部件添加到其他小部件,如Frame。现在你有点乱了。你知道吗

我使用Frame并跳过颜色和字体等不重要的选项。你知道吗

#global variable 
total = 0 # value as start

# to keep all orderde items
#order = []

# to display total value 
#total_label = Label(root, text='')
#total_label.pack()

def blnclick():

    uprice = price.get()
    uitem = item.get()

    global total
    total += int(uprice) # add to global value

    # display new total
    #total_label['text'] = str(total)

    # add ordered item to list
    #order.append({'item': uitem, 'price': uprice})

    frame = Frame(canvas_frme)
    frame.pack()

    l = Label(frame, text=uprice)
    l.grid(row=0, column=0)

    l = Label(frame, text=uitem)
    l.grid(row=1, column=1)

编辑:

完整工作示例

import tkinter as tk

#  - functions  -

def on_button_add():

    uprice = price_entry.get()
    uitem = item_entry.get()

    global total
    total += int(uprice) # add to class value

    # display new total
    total_label['text'] = str(total)

    # add ordered item to list
    order.append({'item': uitem, 'price': uprice})
    print('full order:', order)
    print('total:', total)

    number = len(order)

    l = tk.Label(frame_order, text=uitem)
    l.grid(row=number, column=0)

    l = tk.Label(frame_order, text=uprice)
    l.grid(row=number, column=1)

#  - main  -

# values at start
total = 0
order = []

root = tk.Tk()

# - entry for new item  -

l = tk.Label(root, text='Item:')
l.grid(row=0, column=0)

item_entry = tk.Entry(root)
item_entry.grid(row=0, column=1)

l = tk.Label(root, text='Price:')
l.grid(row=1, column=0)

price_entry = tk.Entry(root)
price_entry.grid(row=1, column=1)

button = tk.Button(root, text='ADD', command=on_button_add)
button.grid(row=2, column=0, columnspan=2)

# - frame with order  -

frame_order = tk.Frame(root)
frame_order.grid(row=3, column=0, columnspan=2)

l = tk.Label(frame_order, text="Items")
l.grid(row=0, column=0)

l = tk.Label(frame_order, text="Prices")
l.grid(row=0, column=1)

# - total  -

# to display total value 
l = tk.Label(root, text='Total:')
l.grid(row=4, column=0)

# to display total value 
total_label = tk.Label(root, text='')
total_label.grid(row=4, column=1)

root.mainloop()

enter image description here

我只需要用一个列表来理解一个列表中的所有数字,并求它们的和

# sample dict 
d = {"one":1, "two":2, "three":3}

# for each key, get it's "price", sum list
x = sum([d[k] for k in d])

相关问题 更多 >