Delete from treeview in tkinter

huangapple go评论65阅读模式
英文:

Delete from treeview in tkinter

问题

这是您要翻译的代码部分:

def delete(self):
    selected_item = self.record_list.selection()
    if selected_item:
        item_code = self.record_list.item(selected_item)["text"]
        db = sql.connect('record.db')
        curs = db.cursor()
        curs.execute("DELETE FROM tb_transfers WHERE code=?", (item_code,))
        db.commit()
        db.close()
        self.record_list_refresh()
        self.total_label_refresh()
        self.withdraw_label_refresh()
        self.deposit_label_refresh()

这部分代码是用于删除所选项目的函数,当您点击删除按钮时,它会从 SQLite 中删除相应的记录。

英文:
import tkinter as tk
from tkinter import ttk
import sqlite3 as sql

class main_window:
    def __init__(self, window):
        self.wind = window
        self.wind.geometry('700x550')
        self.wind.resizable(0, 0)
        self.wind.title('Transfer Record')
        self.wind.iconbitmap('icon.ico')
        self.draw()
        self.record_list_refresh()

    def draw(self):
        data_entry = tk.LabelFrame(self.wind, text= 'Data Entry', bd= 5)
        data_entry.pack(padx= 2, pady= 2, anchor= 'center')

        tk.Label(data_entry, text= 'Description:').grid(row= 0, column= 0)
        self.description = tk.Entry(data_entry, width= 30)
        self.description.grid(row= 1, column= 0, padx= 25)                                       #Description entry

        tk.Label(data_entry, text= 'Kind:').grid(row= 0, column= 1)
        self.kind = ttk.Combobox(data_entry, state= 'readonly')                                                                                #Combobox kind
        self.kind['values'] = ('Deposit', 'Withdrawn')   
        self.kind.current(0)                                                                         
        self.kind.grid(row= 1, column= 1, padx= 25)

        tk.Label(data_entry, text= 'Amount').grid(row= 0, column= 2)
        self.amount = tk.Entry(data_entry)
        self.amount.grid(row= 1, column= 2, padx= 25)                                            #Amount entry

        tk.Button(data_entry, text= 'Add', height= 2, width= 8, bd= 3, command= self.save).grid(row= 0, column= 3, padx= 5, pady= 4)
        tk.Button(data_entry, text= 'Delete', height= 2, width= 8, bd= 3, command= self.delete).grid(row= 1, column= 3, padx= 5, pady= 4)                       #Save button

        transfer_record = tk.LabelFrame(self.wind, text= 'Transfer Record', bd= 5)
        transfer_record.pack(padx= 1, pady= 1, anchor= 'center')

        self.record_list = ttk.Treeview(transfer_record, columns= ('Kind', 'Description', 'Amount'), height= 15)
        self.record_list.column('#0', width= 80, anchor= 'center')
        self.record_list.heading('#0', text= 'Code')
        self.record_list.column('Description', width= 300, anchor= 'center')
        self.record_list.heading('Description', text= 'Description')
        self.record_list.column('Kind', width= 125, anchor= 'center')
        self.record_list.heading('Kind', text= 'Kind')
        self.record_list.column('Amount', width= 80, anchor= 'center')
        self.record_list.heading('Amount', text= 'Amount')
        self.record_list.pack(padx= 5,pady= 4)

        self.totals = tk.LabelFrame(self.wind, text= 'TOTALS', bd= 5)
        self.totals.pack(padx= 1, pady= 1, anchor= 'center')

        self.label_deposit = tk.Label(self.totals, text= '')
        self.label_deposit.pack(side= 'left', padx= 2, pady= 2)
        self.deposit_label_refresh()

        self.label_withdrawn = tk.Label(self.totals, text= '')
        self.label_withdrawn.pack(side= 'left', padx= 200, pady= 2)
        self.withdraw_label_refresh()

        self.label_total= tk.Label(self.totals, text= '')
        self.label_total.pack(side= 'left', padx= 2, pady= 2)
        self.total_label_refresh()

    def record_list_refresh(self):
        self.description.delete(0, tk.END)
        self.amount.delete(0, tk.END)
        for item in self.record_list.get_children():
            self.record_list.delete(item)
        db = sql.connect('record.db')
        curs = db.cursor()

        data = curs.execute("SELECT * FROM tb_transfers")
        data = data.fetchall()
        for code, description, kind, amount in data:
            self.record_list.insert('', tk.END, text= code, values= (description, kind, amount))

        db.commit()
        db.close()

    def deposit_label_refresh(self):
        db = sql.connect('record.db')
        curs = db.cursor()

        deposits = curs.execute("Select amount FROM tb_transfers WHERE kind= 'Deposit'")
        deposits = list(deposits.fetchall())
        deposits_list = []
        for deposit in deposits:
            for depo in deposit:
                deposits_list.append(depo)
        total_deposits = sum(deposits_list)
        total_deposits = str(total_deposits)

        db.commit()
        db.close()
        
        self.label_deposit.configure(text= 'Deposit: $' + total_deposits)
    
    def withdraw_label_refresh(self):
        db = sql.connect('record.db')
        curs = db.cursor()

        withdraws = curs.execute("Select amount FROM tb_transfers WHERE kind= 'Withdrawn'")
        withdraws = list(withdraws.fetchall())
        withdraws_list = []
        for withdraw in withdraws:
            for withd in withdraw:
                withdraws_list.append(withd)
        total_withdraws = sum(withdraws_list)
        total_withdraws = str(total_withdraws)

        db.commit()
        db.close()
        
        self.label_withdrawn.configure(text= 'TOTAL: $' + total_withdraws)
    
    def total_label_refresh(self):
        db = sql.connect('record.db')
        curs = db.cursor()

        deposits = curs.execute("Select amount FROM tb_transfers WHERE kind= 'Deposit'")
        deposits = list(deposits.fetchall())
        deposits_list = []
        for deposit in deposits:
            for depo in deposit:
                deposits_list.append(depo)
        total_deposits = sum(deposits_list)

        withdraws = curs.execute("Select amount FROM tb_transfers WHERE kind= 'Withdrawn'")
        withdraws = list(withdraws.fetchall())
        withdraws_list = []
        for withdraw in withdraws:
            for withd in withdraw:
                withdraws_list.append(withd)
        total_withdraws = sum(withdraws_list)

        total_totals = total_deposits - total_withdraws
        total_totals = str(total_totals)

        db.commit()
        db.close()
        
        self.label_total.configure(text= 'TOTAL: $' + total_totals)

    def save(self):
        db = sql.connect('record.db')
        curs = db.cursor()
        curs.execute("INSERT INTO tb_transfers(description, kind, amount) VALUES(?, ?, ?)", (self.description.get(), self.kind.get(), self.amount.get()))
        db.commit()
        db.close()
        self.record_list_refresh()
        self.total_label_refresh()
        self.withdraw_label_refresh()
        self.deposit_label_refresh()

    def delete(self):
        

obj_main_window = main_window(tk.Tk())
obj_main_window.wind.mainloop()

the idea is delete the selected item from the sqlite when i press the delete button.

答案1

得分: 0

You can get the selected code using Treeview.focus() and Treeview.item():

def delete(self):
    selected = self.record_list.focus()  # item selected?
    if selected:
        # 获取所选项的 'code'
        code = self.record_list.item(selected, "text")
        # 从 sqlite 表中删除记录
        db = sql.connect("record.db")
        cur = db.cursor()
        cur.execute("DELETE FROM tb_transfers WHERE code = ?", (code,))
        db.commit()
        db.close()
        # 更新 GUI
        self.record_list_refresh()
        self.total_label_refresh()
        self.withdraw_label_refresh()
        self.deposit_label_refresh()
英文:

You can get the selected code using Treeview.focus() and Treevew.item():

def delete(self):
    selected = self.record_list.focus()  # item selected?
    if selected:
        # get the 'code' of selected item
        code = self.record_list.item(selected, "text")
        # remove record from sqlite table
        db = sql.connect("record.db")
        cur = db.cursor()
        cur.execute("DELETE FROM tb_transfers WHERE code = ?", (code,))
        db.commit()
        db.close()
        # update GUI
        self.record_list_refresh()
        self.total_label_refresh()
        self.withdraw_label_refresh()
        self.deposit_label_refresh()

huangapple
  • 本文由 发表于 2023年5月11日 01:08:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/76220999.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定