如何使用python计算总列数

2024-03-28 23:11:29 发布

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

我正在用Python创建一个清单系统。Treeview表total列需要使用python计算和显示总和。我需要计算总计列的最终总计。我试过了,但我可以得到我迄今为止所尝试的结果,我将其附在下面。我犯了个错误

sum1 += tot TypeError: unsupported operand type(s) for +=: 'float' and 'tuple'

我需要在上面的屏幕截图中计算列tot值,如400+5000+900;我显示我必须打印最终总数

from tkinter import *
from tkinter import ttk
import mysql.connector


def show():

    tot = 0

    if(var1.get()):

      price = int(e1.get())
      qty = int(e6.get())
      tot = int(price * qty)

      tempList = [['Thai Fried Rice', e1.get(), e6.get(), tot]]
      tempList.sort(key=lambda e: e[1], reverse=True)

      for i, (item, price, qty, tot) in enumerate(tempList,start=1):
       listBox.insert("", "end", values=(item, price, qty, tot))

    if (var2.get()):

        price = int(e2.get())
        qty = int(e7.get())
        tot = int(price * qty)

        tempList = [['Basil Fried Rice', e2.get(), e7.get(), tot]]
        tempList.sort(key=lambda e: e[1], reverse=True)

        for i, (item, price, qty, tot) in enumerate(tempList, start=1):
            listBox.insert("", "end", values=(item, price, qty, tot))

    if (var3.get()):

        price = int(e3.get())
        qty = int(e8.get())
        tot = int(price * qty)

        tempList = [['Pineapple Fried Rice', e3.get(), e8.get(), tot]]
        tempList.sort(key=lambda e: e[1], reverse=True)

        for i, (item, price, qty, tot) in enumerate(tempList, start=1):
            listBox.insert("", "end", values=(item, price, qty, tot))

    if (var4.get()):

        price = int(e4.get())
        qty = int(e9.get())
        tot = int(price * qty)

        tempList = [['Crab Fried Rice', e4.get(), e9.get(), tot]]
        tempList.sort(key=lambda e: e[1], reverse=True)

        for i, (item, price, qty, tot) in enumerate(tempList, start=1):
            listBox.insert("", "end", values=(item, price, qty, tot))

    if (var5.get()):

        price = int(e5.get())
        qty = int(e10.get())
        tot = int(price * qty)

        tempList = [['Fish Fried Rice', e5.get(), e10.get(), tot]]
        tempList.sort(key=lambda e: e[1], reverse=True)

        for i, (item, price, qty, tot) in enumerate(tempList, start=1):
            listBox.insert("", "end", values=(item, price, qty, tot))

    sum1 = 0.0

    for tot in enumerate(tempList):
        sum1 += tot

    print(sum1)

Tags: keyinforgetifitemsortprice
1条回答
网友
1楼 · 发布于 2024-03-28 23:11:29

这样行吗?将show()中的最后一个for循环更改为:

for child in listBox.get_children():
        sum1 += float(listBox.item(child,'values')[3])
print(sum1)

希望它解决了您的疑问,如果有任何错误请告诉我

干杯

相关问题 更多 >