Skip to content
Advertisement

How to calculate total column using python

I am creating an inventory system in Python. Treeview table total column need to calculate and display sum using python. I need to calculate final total of total column. I tried but I could the result what I tried so far I attached below. I got an error

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

I need to calculate column tot values like 400 + 5000 + 900 in above screenshot; I shown I have to print the final total

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)

Advertisement

Answer

Does something like this work? Change the last for loop in show() to this:

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

Hope it solved your doubts, if any errors do let me know

Cheers

Advertisement