Python Tkinter Treeview Column Values Sum

How to Sum all The Numbers in a Tkinter Treeview Column Using Python

Python Tkinter Treeview Column Values Sum


import tkinter as tk
from tkinter import *
from tkinter import ttk


root = Tk()
root.title("MAX-MIN-SUM-AVG")

frame_container = tk.Frame(root, padx=10, pady=10, bg='#badc58')
frame_fields = tk.Frame(frame_container, bg='#badc58')

# create treeview
trv = ttk.Treeview(frame_container, columns=(1,2,3,4), show='headings')

trv.column(1, anchor='center', width=100)
trv.column(2, anchor='center', width=100)
trv.column(3, anchor='center', width=100)
trv.column(4, anchor='center', width=100)

trv.heading(1, text='ID')
trv.heading(2, text='Name')
trv.heading(3, text='Quantity')
trv.heading(4, text='Price')

# add items to the treeview
trv.insert("",'end', iid=1, values=(1,"Product 1",100, 10))
trv.insert("",'end', iid=2, values=(2,"Product 2",200, 20))
trv.insert("",'end', iid=3, values=(3,"Product 3",300, 30))
trv.insert("",'end', iid=4, values=(4,"Product 4",400, 400))
trv.insert("",'end', iid=5, values=(5,"Product 5",500, 50))
trv.insert("",'end', iid=6, values=(6,"Product 6",600, 60))

# sum value
sum_lbl = tk.Label(frame_fields, text='Sum:', font=('Verdana',14), bg='#badc58')
sum_entry = tk.Entry(frame_fields, font=('Verdana',14))

sum_lbl.grid(row=0, column=0, padx=10, pady=10, sticky='e')
sum_entry.grid(row=0, column=1)


# function to display the sum value
def getSum(item=""):
val = 0
for row in trv.get_children(item):
#print(trv.item(row)["values"][3])# print price
val = val + trv.item(row)["values"][3]
print(val)
sum_entry.insert(0,val)



getSum()

trv.pack()
frame_container.pack()
frame_fields.pack()

root.mainloop()


OUTPUT:

Sum all The Numbers in a Tkinter Treeview Column Using Python






Share this

Related Posts

Previous
Next Post »