简体   繁体   中英

Calculating the total of a list of payments that is updated every time a value is added or deleted in treeview

I am writing a Tkinter code to continuously calculate and display the sum of multiple values in a treeview, that has to update the total every time a new value is added or deleted. The addition works well but when I delete a row, the total is not calculated properly: Here is my code:

def buying_addcheck(self):
       self.buying_totalchecks = 0
      # UNESSESRY CODE#

      #CODE FOR ADDED VALUES#
       for child in self.buying_checkspaymenttree.get_children():
            self.buying_totalchecks += int(self.buying_checkspaymenttree.item(child)["values"][1])

            self.subtraction = int(self.pricevar.get()) - self.buying_totalchecks
            self.carprice_Verif_l.config(text=f"{self.pricevar.get()}: PRICE")
            self.checks_Verif_l.config(text=f"{self.buying_totalchecks}: TOTAL")
            self.checks_subtraction_l.config(text=f"{self.subtraction}: REAMINING")


def buying_deletecheck(self):
       #CODE FOR SUBSTRACT VALUES#
        self.con = sqlite3.connect('car dealership.db')
        self.cursorObj = self.con.cursor()
        self.buying_selected_item_deletecheck = self.buying_checkspaymenttree.selection()[0]
        # print(self.buying_checkspaymenttree.item(self.buying_selected_item_deletecheck)['values'])
        uid = self.buying_checkspaymenttree.item(self.buying_selected_item_deletecheck)['values'][3]

self.cursorObj.execute("DELETE FROM cars_buying_checks WHERE id=?", (uid,))
        self.con.commit()
        self.rowid -= 1
        self.buying_checkspaymenttree.delete(self.buying_selected_item_deletecheck)
        for child in self.buying_checkspaymenttree.get_children():
            self.buying_totalchecks += int(self.buying_checkspaymenttree.item(child)["values"][1])

            self.subtraction = int(self.pricevar.get()) - self.buying_totalchecks
            self.carprice_Verif_l.config(text=f"{self.pricevar.get()}: PRICE")
            self.checks_Verif_l.config(text=f"{self.buying_totalchecks}: TOTAL")
            self.checks_subtraction_l.config(text=f"{self.subtraction}: REAMINING")

Since both buying_addcheck() and buying_deletecheck() need to calculate the total, I would suggest to move the calculation of the total to a function and call that function inside the above two functions:

# function to calculate the total
def buying_calctotal(self):
    self.buying_totalchecks = 0
    for child in self.buying_checkspaymenttree.get_children():
        self.buying_totalchecks += int(self.buying_checkspaymenttree.item(child)["values"][1])

    self.subtraction = int(self.pricevar.get()) - self.buying_totalchecks
    self.carprice_Verif_l.config(text=f"{self.pricevar.get()}: PRICE")
    self.checks_Verif_l.config(text=f"{self.buying_totalchecks}: TOTAL")
    self.checks_subtraction_l.config(text=f"{self.subtraction}: REAMINING")

def buying_addcheck(self):
    # code for add value
    ...
    # calculate total
    self.buying_calctotal()

def buying_deletecheck(self):
    # code for substract value
    self.con = sqlite3.connect('car dealership.db')
    self.cursorObj = self.con.cursor()
    self.buying_selected_item_deletecheck = self.buying_checkspaymenttree.selection()[0]
    # print(self.buying_checkspaymenttree.item(self.buying_selected_item_deletecheck)['values'])
    uid = self.buying_checkspaymenttree.item(self.buying_selected_item_deletecheck)['values'][3]
    self.cursorObj.execute("DELETE FROM cars_buying_checks WHERE id=?", (uid,))
    self.con.commit()
    self.rowid -= 1
    self.buying_checkspaymenttree.delete(self.buying_selected_item_deletecheck)
    # calculate total
    self.buying_calctotal()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM