首页 > 解决方案 > 如何使用python计算总列

问题描述

我正在用 Python 创建一个库存系统。Treeview 表格总列需要使用 python 计算和显示总和。我需要计算总列的最终总数。我试过了,但我可以得到我到目前为止所尝试的结果,我附在下面。我有一个错误

sum1 += tot TypeError: += 不支持的操作数类型: 'float' 和 'tuple'

我需要在上面的屏幕截图中计算像 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)

标签: pythontkinter

解决方案


这样的事情有用吗?将最后一个for循环更改为show()

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

希望它能解决你的疑惑,如果有任何错误请告诉我

干杯


推荐阅读