我正在用 Python 創(chuàng)建一個庫存系統(tǒng)。Treeview 表總計列需要使用 python 計算和顯示總和。我需要計算總計列的最終總數(shù)。我試過了,但我可以得到我到目前為止所嘗試的結(jié)果,我附在下面。我有一個錯誤sum1 += tot TypeError: +=: 'float' 和 'tuple' 不支持的操作數(shù)類型我需要在上面的屏幕截圖中計算像 400 + 5000 + 900 這樣的列 tot 值;我表明我必須打印最終總數(shù)from tkinter import *from tkinter import ttkimport mysql.connectordef 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))
1 回答

手掌心
TA貢獻1942條經(jīng)驗 獲得超3個贊
這樣的東西有用嗎?將最后一個for循環(huán)更改show()為:
for child in listBox.get_children():
sum1 += float(listBox.item(child,'values')[3])
print(sum1)
希望它解決了你的疑惑,如果有任何錯誤請告訴我
添加回答
舉報
0/150
提交
取消