Java, Python, Database, Flutter, Matlap, Micorcontroller, Tutorials, Swing Framework

Hancie e-Learning Studio

Learn Java, Learn HTML, CSS, PHP, Javascript, Python Tutorials || Download program source codes || Java Project and Source code available here || All types error troubleshooting tips available here

How to use Python to retrieve the Average Value in a Tkinter Treeview Column?

 


from tkinter import *
from tkinter import ttk
from dbms.billing_backend import billing_history12

class Hancie():
    def __init__(self, main):
        self.main=main
        self.main.title("Hancie e-Learning Studio")
        self.main.resizable(0, 0)
        frame_width = 1050
        frame_height = 500
        screen_width = self.main.winfo_screenwidth()
        screen_height = self.main.winfo_screenheight()
        x_cordinate = int((screen_width / 2) - (frame_width / 2))
        y_cordinate = int((screen_height / 2) - (frame_height / 2))
        self.main.geometry('{}x{}+{}+{}'.format(frame_width, frame_height, x_cordinate+50, y_cordinate+50))

        treeView=ttk.Treeview(self.main)
        treeView.pack(side=BOTTOM, fill=BOTH, expand=True)
        treeView['columns']=('bookingid', 'name','km','unit','total')
        treeView.column('#0', width=0, stretch=0)
        treeView.column('bookingid', width=100, anchor=CENTER)
        treeView.column('name', width=200, anchor=CENTER)
        treeView.column('km', width=100, anchor=CENTER)
        treeView.column('unit', width=100, anchor=CENTER)
        treeView.column('total', width=150, anchor=CENTER)

        treeView.heading('#0', text='', anchor=CENTER)
        treeView.heading('bookingid',text='ID', anchor=CENTER)
        treeView.heading('name', text='Name', anchor=CENTER)
        treeView.heading('km', text='KM', anchor=CENTER)
        treeView.heading('unit', text='Unit', anchor=CENTER)
        treeView.heading('total', text='Total', anchor=CENTER)



        def billingtable():

            historyResult=billing_history12()

            for ro in historyResult:
                treeView.insert(parent='', index='end', values=(ro[0],ro[1],ro[6],ro[7],ro[8]))


        billingtable()

        def getaverage(item=""):
            val = 0
            for row in treeView.get_children(item):
                # print(trv.item(row)["values"][3])# print price
                val = val + treeView.item(row)["values"][4]
                average = val / len(treeView.get_children())

            treeView.insert(parent='', index='end', values=('','','','',"Average: {}".format(average)))

        getaverage()



if __name__=='__main__':
    main=Tk()
    Hancie(main)
    main.mainloop()
Output

Welcome all to Hancie e-learning studio
Friends,
I have brought an e-learning platform for you from where you can teach internet, website, programming language, error troubleshooting, etc. Blogger, WordPress templates, themes are available for free on this website, which are not charged for downloading, so that you can earn money by blogging using such templates and this is not a difficult task and this You can do it now. You keep uploading new posts by us and keep taking advantage of this website. The aim of which is to spread the knowledge related to internet, career, web designing and technology to the people and contribute to the development of the country.