帮我写一个记账小程序代码,要实现以下内容:(1)使用类实现数据封装; (2)使用tkinter实现界面UI设计。 (3)使用文件保存数据。 (4)能实现用户登陆功能和实现数据的增加、修改、删除和查询操作。 (5)开发平台:Python 3.7以上版本等软件。 (6)项目选题:开题前需向任何教师确认题目及开发难度是否符合课程要求。 (7)作品要求:界面美观易操作,人机交互符合商业应用流程。
时间: 2024-03-09 10:47:01 浏览: 50
记账小程序
好的,下面是一个简单的记账小程序代码,满足你的要求:
```python
import tkinter as tk
import json
class User:
def __init__(self, username, password):
self.username = username
self.password = password
class Account:
def __init__(self, date, amount, category, note):
self.date = date
self.amount = amount
self.category = category
self.note = note
class App:
def __init__(self, master):
self.master = master
master.geometry("400x300")
master.title("记账小程序")
self.logged_in = False
self.current_user = None
self.accounts = []
# 登录界面
self.login_frame = tk.Frame(master)
self.login_frame.pack()
self.username_label = tk.Label(self.login_frame, text="用户名:")
self.username_label.grid(row=0, column=0)
self.username_entry = tk.Entry(self.login_frame)
self.username_entry.grid(row=0, column=1)
self.password_label = tk.Label(self.login_frame, text="密码:")
self.password_label.grid(row=1, column=0)
self.password_entry = tk.Entry(self.login_frame, show="*")
self.password_entry.grid(row=1, column=1)
self.login_button = tk.Button(self.login_frame, text="登录", command=self.login)
self.login_button.grid(row=2, column=0)
self.register_button = tk.Button(self.login_frame, text="注册", command=self.register)
self.register_button.grid(row=2, column=1)
# 主界面
self.main_frame = tk.Frame(master)
self.main_frame.pack()
self.date_label = tk.Label(self.main_frame, text="日期:")
self.date_label.grid(row=0, column=0)
self.date_entry = tk.Entry(self.main_frame)
self.date_entry.grid(row=0, column=1)
self.amount_label = tk.Label(self.main_frame, text="金额:")
self.amount_label.grid(row=1, column=0)
self.amount_entry = tk.Entry(self.main_frame)
self.amount_entry.grid(row=1, column=1)
self.category_label = tk.Label(self.main_frame, text="类别:")
self.category_label.grid(row=2, column=0)
self.category_entry = tk.Entry(self.main_frame)
self.category_entry.grid(row=2, column=1)
self.note_label = tk.Label(self.main_frame, text="备注:")
self.note_label.grid(row=3, column=0)
self.note_entry = tk.Entry(self.main_frame)
self.note_entry.grid(row=3, column=1)
self.add_button = tk.Button(self.main_frame, text="添加", command=self.add_account)
self.add_button.grid(row=4, column=0)
self.modify_button = tk.Button(self.main_frame, text="修改", command=self.modify_account)
self.modify_button.grid(row=4, column=1)
self.delete_button = tk.Button(self.main_frame, text="删除", command=self.delete_account)
self.delete_button.grid(row=4, column=2)
self.query_button = tk.Button(self.main_frame, text="查询", command=self.query_account)
self.query_button.grid(row=4, column=3)
self.accounts_listbox = tk.Listbox(self.main_frame)
self.accounts_listbox.grid(row=5, column=0, columnspan=4)
self.load_data()
def login(self):
username = self.username_entry.get()
password = self.password_entry.get()
with open("users.json", "r") as f:
users = json.load(f)
for user in users:
if user['username'] == username and user['password'] == password:
self.logged_in = True
self.current_user = User(username, password)
self.show_main_frame()
break
else:
tk.messagebox.showerror("错误", "用户名或密码错误")
def register(self):
username = self.username_entry.get()
password = self.password_entry.get()
with open("users.json", "r") as f:
users = json.load(f)
for user in users:
if user['username'] == username:
tk.messagebox.showerror("错误", "用户名已存在")
break
else:
users.append({'username': username, 'password': password})
with open("users.json", "w") as f:
json.dump(users, f)
tk.messagebox.showinfo("提示", "注册成功,请登录")
def show_main_frame(self):
if self.logged_in:
self.login_frame.pack_forget()
self.main_frame.pack()
self.master.title(f"记账小程序 - {self.current_user.username}")
def add_account(self):
if self.logged_in:
date = self.date_entry.get()
amount = self.amount_entry.get()
category = self.category_entry.get()
note = self.note_entry.get()
account = Account(date, amount, category, note)
self.accounts.append(account)
self.save_data()
self.show_accounts()
def modify_account(self):
if self.logged_in:
selection = self.accounts_listbox.curselection()
if len(selection) == 1:
index = selection[0]
account = self.accounts[index]
account.date = self.date_entry.get()
account.amount = self.amount_entry.get()
account.category = self.category_entry.get()
account.note = self.note_entry.get()
self.save_data()
self.show_accounts()
def delete_account(self):
if self.logged_in:
selection = self.accounts_listbox.curselection()
if len(selection) == 1:
index = selection[0]
del self.accounts[index]
self.save_data()
self.show_accounts()
def query_account(self):
if self.logged_in:
category = self.category_entry.get()
filtered_accounts = []
for account in self.accounts:
if account.category == category:
filtered_accounts.append(account)
self.show_accounts(filtered_accounts)
def show_accounts(self, accounts=None):
if accounts is None:
accounts = self.accounts
self.accounts_listbox.delete(0, tk.END)
for account in accounts:
text = f"{account.date} {account.amount} {account.category} {account.note}"
self.accounts_listbox.insert(tk.END, text)
def save_data(self):
data = []
for account in self.accounts:
data.append({
'date': account.date,
'amount': account.amount,
'category': account.category,
'note': account.note
})
with open(f"{self.current_user.username}.json", "w") as f:
json.dump(data, f)
def load_data(self):
with open("users.json", "r") as f:
users = json.load(f)
if len(users) == 0:
users.append({'username': 'admin', 'password': 'admin'})
with open("users.json", "w") as f:
json.dump(users, f)
with open(f"{self.current_user.username}.json", "a+") as f:
f.seek(0)
data = f.read()
if len(data) == 0:
data = "[]"
self.accounts = json.loads(data)
self.show_accounts()
root = tk.Tk()
app = App(root)
root.mainloop()
```
这个程序实现了一个简单的记账小应用程序,包括了用户登录、账目增加、修改、删除和查询等功能。数据使用json格式保存在文件中。你可以根据自己的需要进行修改和完善。希望对你有所帮助!
阅读全文