python - 如何使用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)
解决方案
这样的事情有用吗?将最后一个for
循环更改为show()
:
for child in listBox.get_children():
sum1 += float(listBox.item(child,'values')[3])
print(sum1)
希望它能解决你的疑惑,如果有任何错误请告诉我
干杯
推荐阅读
- python - 如何导入模块 kicost 并传递参数
- php - 更新到 composer 2.0 后 TravisCI 版本矩阵出现问题
- r - 如何使用每个唯一过滤器编写多个 CSV
- android - 通过 WiFi 与设备进行本地连接并从 android 发布数据
- python - 如何根据来自两个数据框的两列或三列之间的条件创建一个新的布尔列?
- linked-list - 为什么这个链接列表中的头部会发生变化?
- python - 为什么我在运行时会在 kafka producer 中收到错误?
- r - 避免在 R Shiny 中的反应式表达式中执行耗时的步骤
- java - NoSuchMethodError: 没有静态方法 getLogger(Ljava/lang/String;)Lsun/util/logging/PlatformLogger; 在类 Lsun/util/logging/PlatformLogger
- php - 在php/mysql中计算销售应用程序的损益(每日、每月和每年)