程序演示
添加记账
编辑记账
删除记账
代码解释
一、导入模块
import tkinter as tk
from tkinter import messagebox
tkinter: 用于创建GUI应用的库。
tkinter.messagebox: 用于显示消息框的库。
二、定义Expense类
class Expense:
def __init__(self, date, category, amount):
self.date = date
self.category = category
self.amount = amount
date: 开支的日期。
category: 开支的类别。
amount: 开支的金额。
三、定义ExpenseTracker类
class ExpenseTracker:
def __init__(self):
self.expenses = []
def add_expense(self, expense):
self.expenses.append(expense)
def remove_expense(self, expense):
self.expenses.remove(expense)
def update_expense(self, expense, new_date, new_category, new_amount):
expense.date = new_date
expense.category = new_category
expense.amount = new_amount
expenses: 存储Expense对象的列表,用于跟踪所有开支。
add_expense: 向列表中添加一个新的开支。
remove_expense: 从列表中移除一个开支。
update_expense: 更新列表中的开支信息。
四、定义GUI类
class GUI:
def __init__(self, root):
self.root = root
self.expense_tracker = ExpenseTracker()
self.root.geometry("500x400")
self.date_label = tk.Label(root, text="日期:")
self.date_label.pack()
self.date_entry = tk.Entry(root,width=40,)
self.date_entry.pack()
self.category_label = tk.Label(root, text="记账类别:")
self.category_label.pack()
self.category_entry = tk.Entry(root,width=40,)
self.category_entry.pack()
self.amount_label = tk.Label(root, text="金额数量:")
self.amount_label.pack()
self.amount_entry = tk.Entry(root,width=40,)
self.amount_entry.pack()
self.add_button = tk.Button(root, text="添加记账", command=self.add_expense)
self.add_button.pack()
self.expenses_listbox = tk.Listbox(root,width=40,)
self.expenses_listbox.pack()
self.edit_button = tk.Button(root, text="编辑记账", command=self.edit_expense)
self.edit_button.pack()
self.delete_button = tk.Button(root, text="删除记账", command=self.delete_expense)
self.delete_button.pack()
self.load_expenses()
def load_expenses(self):
self.expenses_listbox.delete(0, tk.END)
for expense in self.expense_tracker.expenses:
self.expenses_listbox.insert(tk.END, f"{expense.date} - {expense.category} - {expense.amount}元")
def add_expense(self):
date = self.date_entry.get()
category = self.category_entry.get()
amount = self.amount_entry.get()
expense = Expense(date, category, amount)
self.expense_tracker.add_expense(expense)
self.date_entry.delete(0, tk.END)
self.category_entry.delete(0, tk.END)
self.amount_entry.delete(0, tk.END)
self.load_expenses()
def edit_expense(self):
selected_index = self.expenses_listbox.curselection()
if selected_index:
expense = self.expense_tracker.expenses[selected_index[0]]
edit_window = tk.Toplevel(self.root)
date_label = tk.Label(edit_window, text="日期:")
date_label.pack()
date_entry = tk.Entry(edit_window)
date_entry.pack()
date_entry.insert(tk.END, expense.date)
category_label = tk.Label(edit_window, text="记账类别:")
category_label.pack()
category_entry = tk.Entry(edit_window)
category_entry.pack()
category_entry.insert(tk.END, expense.category)
amount_label = tk.Label(edit_window, text="金额数量:")
amount_label.pack()
amount_entry = tk.Entry(edit_window)
amount_entry.pack()
amount_entry.insert(tk.END, expense.amount)
save_button = tk.Button(edit_window, text="保存", command=lambda: self.save_expense(expense, date_entry.get(), category_entry.get(), amount_entry.get(), edit_window))
save_button.pack()
else:
messagebox.showinfo("错误", "没有记账被选中")
def save_expense(self, expense, new_date, new_category, new_amount, edit_window):
self.expense_tracker.update_expense(expense, new_date, new_category, new_amount)
edit_window.destroy()
self.load_expenses()
def delete_expense(self):
selected_index = self.expenses_listbox.curselection()
if selected_index:
expense = self.expense_tracker.expenses[selected_index[0]]
self.expense_tracker.remove_expense(expense)
self.load_expenses()
else:
messagebox.showinfo("错误", "没有记账被选中")
__init__方法:初始化GUI对象,创建窗口、标签、输入框和按钮等界面元素。
load_expenses方法:加载已有的记账信息并显示在列表中。
add_expense方法:添加新的记账信息到ExpenseTracker对象中,并在列表中更新显示。
edit_expense方法:打开一个编辑窗口,允许用户修改已有的记账信息。
save_expense方法:保存编辑后的记账信息到ExpenseTracker对象中。
delete_expense方法:删除选定的记账信息。
五、__main__部分
if __name__ == "__main__":
root = tk.Tk()
root.title("个人记帐本")
gui = GUI(root)
root.mainloop()
创建一个tkinter的主窗口并设置其标题。
创建一个GUI类的实例并运行tkinter的主循环。