sqlite 从tkinter中的treeview中删除

a64a0gku  于 2023-05-18  发布在  SQLite
关注(0)|答案(1)|浏览(178)
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()

这个想法是删除所选项目从SQLite当我按下删除按钮。

8ljdwjyq

8ljdwjyq1#

您可以使用Treeview.focus()Treevew.item()获取所选的code

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()

相关问题