Skip to content Skip to sidebar Skip to footer

Python How To Sum All The Numbers In A Treeview Column

I need to sum all the numbers of the 'Total Sum' Column of Treeview: The code is: from tkinter import ttk import tkinter as tk from tkinter import* def update(): listBox.inse

Solution 1:

Here is a method to do what you want,

Start of by making a button and a function to it:

b1 = tk.Button(root,text='Add Listbox',command=add)
b1.grid(row=4)

then the add() can be something like:

def add():
    total = 0.0

    for child in listBox.get_children():
        total += float(listBox.item(child, 'values')[3])
    
    lbl = Label(root,text=total,font=('helvetica',18))
    lbl.grid(row=5)

This will print the total of the item in the final column.

Tip:

You can insert the sum of entry box automatically onto the third entry box.

defadded():
    # Sets the sum of values of e1 and e2 as val of e3try :
        sum_tk.set((int(t1.get().replace(' ', '')) + int(t2.get().replace(' ', ''))))
    except :
        pass
    
    root.after(1, added) # reschedule the eventreturn

Then you should define something like and change the code to below.

sum_tk = tk.StringVar()
t3 = tk.Entry(root,textvariable=sum_tk)
t3.grid(row=1,column=3)

and towards the end of the code, add

root.after(1,added)

This will call the function when the code begins.

Above extra answer taken from here

If you dont want buttons, then remove the button and its function and just change your update() as below.

def update():
    if t1.get() == '' or t2.get() == '' or t3.get() == '':
        pass
    else:
        listBox.insert('','end',value=('APL', t1.get(),t2.get(),t3.get()))
    total = 0.0
    try:
        for child in listBox.get_children():
            total += float(listBox.item(child, 'values')[3])
    except:
        pass
    print(total)
    lbl = Label(root,text=total,font=('helvetica',21))
    lbl.grid(row=5)

    t1.delete(0,'end')
    t2.delete(0,'end')
    t3.delete(0,'end')

Hope this helped, do let me know if any doubts or errors.

Cheers

Post a Comment for "Python How To Sum All The Numbers In A Treeview Column"