本次程序用时3小时,目的 ,本质是一次对AI的调教产物,利用AI实现我的需求,一步步完成所有代码,其中背景部分所用时间最长,最后是自己手动解决的,在对于程序页面的上下,背景等词AI貌似还不算多能理解。最后除背景图片部分是根据上一个程序的背景(csdn找到的一个大神的思路),其余都是AI初步写出,然后我自己在源代码的基础上修改而成。
记账本包括功能:主页面显示最近十条记录,多个功能性按钮。包括收入和支出记录的填写页面,修改信息的功能,查询某年某月(可优化的更加详细)的所有记账信息并显示,查询某年某月某日,某年某月,某年的全部信息,然后统计收入和输出并显示。
以下是代码:
1.主页面
import tkinter as tk
import subprocess
root = tk.Tk()
root.title('记账本')
root.geometry('500x500')
root.maxsize(500, 500)
root.minsize(500, 500)
canvas = tk.Canvas(root, width=500, height=500, bg=None)
canvas.pack()
image_file = tk.PhotoImage(file="rrr.png")
image = canvas.create_image(250, 250, anchor='center', image=image_file)
text_box = tk.Text(root, height=20, width=50)
text_box.place(x=50, y=20)
def read_last_lines(file_path, num_lines=10):
try:
with open(file_path, 'r', encoding='utf-8') as file:
lines = file.readlines()
last_lines = lines[-num_lines:]
return ''.join(last_lines)
except Exception as e:
return f"Error reading file: {e}"
file_path = '记账信息.txt'
file_content = read_last_lines(file_path)
text_box.insert(tk.END, file_content)
def c1():
subprocess.run(["python", "收入页面.py"])
def c2():
subprocess.run(["python", "支出页面.py"])
def c3():
subprocess.run(["python", "收支记录页面.py"])
def c4():
subprocess.run(["python", "删除页面.py"])
def c5():
subprocess.run(["python", "修改页面.py"])
def c6():
file_content = read_last_lines(file_path)
text_box.delete('1.0', tk.END)
text_box.insert(tk.END, file_content)
def c7():
subprocess.run(["python", "收支统计页面.py"])
bt3 = tk.Button(root, text='收支记录', command=c3)
bt3.place(relx=0.20, rely=0.64)
bt1 = tk.Button(root, text='收入', command=c1)
bt1.place(relx=0.35, rely=0.64)
bt2 = tk.Button(root, text='支出', command=c2)
bt2.place(relx=0.45, rely=0.64)
bt4 = tk.Button(root, text='删除', command=c4)
bt4.place(relx=0.55, rely=0.64)
bt5 = tk.Button(root, text='修改', command=c5)
bt5.place(relx=0.65, rely=0.64)
bt6 = tk.Button(root, text='刷新', command=c6)
bt6.place(relx=0.1, rely=0.64)
bt7 = tk.Button(root, text='刷新', command=c7)
bt7.place(relx=0.75, rely=0.64)
root.mainloop()
2.收入页面
from tkinter import *
from tkinter import messagebox
root = Tk()
root.title('请填写:')
root.geometry('400x400+700+300')
date_label = Label(root, text='日期:', font=('黑体', 12))
date_label.grid(row=1, column=0, pady=10, padx=(0, 0), sticky=W)
year_var = StringVar()
month_var = StringVar()
day_var = StringVar()
id_var = StringVar()
year_label = Label(root, text='年:', font=('黑体', 12))
year_label.grid(row=1, column=1, pady=10, padx=(0, 0), sticky=W)
year_entry = Entry(root, textvariable=year_var, font=('黑体', 12), width=5)
year_entry.grid(row=1, column=2, pady=10)
month_label = Label(root, text='月:', font=('黑体', 12))
month_label.grid(row=1, column=3, pady=10, sticky=W)
month_entry = Entry(root, textvariable=month_var, font=('黑体', 12), width=3)
month_entry.grid(row=1, column=4, pady=10)
day_label = Label(root, text='日:', font=('黑体', 12))
day_label.grid(row=1, column=5, pady=10, sticky=W)
day_entry = Entry(root, textvariable=day_var, font=('黑体', 12), width=3)
day_entry.grid(row=1, column=6, pady=10)
day_label = Label(root, text='编号:', font=('黑体', 12))
day_label.grid(row=1, column=7, pady=10, sticky=W)
day_entry = Entry(root, textvariable=id_var, font=('黑体', 12), width=3)
day_entry.grid(row=1, column=8, pady=10)
je = StringVar()
rql = Label(root, text='金额:', font=('黑体', 12))
rql.grid(row=2, column=0, pady=10, sticky=W)
rqk = Entry(root, textvariable=je, font=('黑体', 12))
rqk.grid(row=2, column=1, pady=10, columnspan=5)
lb = StringVar()
rql = Label(root, text='类别:', font=('黑体', 12))
rql.grid(row=3, column=0, pady=10, sticky=W)
rqk = Entry(root, textvariable=lb, font=('黑体', 12))
rqk.grid(row=3, column=1, pady=10, columnspan=5)
bz = StringVar()
rql = Label(root, text='备注:', font=('黑体', 12))
rql.grid(row=4, column=0, pady=10, sticky=W)
rqk = Entry(root, textvariable=bz, font=('黑体', 12))
rqk.grid(row=4, column=1, pady=10, columnspan=5)
bt1 = Button(root, text='提交', font=('黑体', 12))
bt1.grid(row=5, column=0, columnspan=6, padx=(20, 10), pady=10)
def w(event):
global year_var, month_var, day_var, je, lb, bz,id_var
year = year_var.get()
month = month_var.get()
day = day_var.get()
id=id_var.get()
je1 = je.get()
lb1 = lb.get()
bz1 = bz.get()
file = '记账信息.txt'
with open(file, 'a', encoding='utf-8') as file_write:
file_write.write(f"{year}.{month}.{day}.{id} ")
file_write.write(f"金额:{je1} ")
file_write.write(f"类别:{lb1} ")
file_write.write(f"备注:{bz1}\n")
name = ''
text = '记录成功!'
messagebox.showinfo(name, text)
root.destroy()
bt1.bind('<Button-1>', w)
root.mainloop()
3.支出页面:
from tkinter import *
from tkinter import messagebox
root = Tk()
root.title('请填写:')
root.geometry('400x400+700+300')
date_label = Label(root, text='日期:', font=('黑体', 12))
date_label.grid(row=1, column=0, pady=10, padx=(0, 0), sticky=W)
year_var = StringVar()
month_var = StringVar()
day_var = StringVar()
id_var = StringVar()
year_label = Label(root, text='年:', font=('黑体', 12))
year_label.grid(row=1, column=1, pady=10, padx=(0, 0), sticky=W)
year_entry = Entry(root, textvariable=year_var, font=('黑体', 12), width=5)
year_entry.grid(row=1, column=2, pady=10)
month_label = Label(root, text='月:', font=('黑体', 12))
month_label.grid(row=1, column=3, pady=10, sticky=W)
month_entry = Entry(root, textvariable=month_var, font=('黑体', 12), width=3)
month_entry.grid(row=1, column=4, pady=10)
day_label = Label(root, text='日:', font=('黑体', 12))
day_label.grid(row=1, column=5, pady=10, sticky=W)
day_entry = Entry(root, textvariable=day_var, font=('黑体', 12), width=3)
day_entry.grid(row=1, column=6, pady=10)
day_label = Label(root, text='编号:', font=('黑体', 12))
day_label.grid(row=1, column=7, pady=10, sticky=W)
day_entry = Entry(root, textvariable=id_var, font=('黑体', 12), width=3)
day_entry.grid(row=1, column=8, pady=10)
je = StringVar()
rql = Label(root, text='金额:', font=('黑体', 12))
rql.grid(row=2, column=0, pady=10, sticky=W)
rqk = Entry(root, textvariable=je, font=('黑体', 12))
rqk.grid(row=2, column=1, pady=10, columnspan=5)
lb = StringVar()
rql = Label(root, text='类别:', font=('黑体', 12))
rql.grid(row=3, column=0, pady=10, sticky=W)
rqk = Entry(root, textvariable=lb, font=('黑体', 12))
rqk.grid(row=3, column=1, pady=10, columnspan=5)
bz = StringVar()
rql = Label(root, text='备注:', font=('黑体', 12))
rql.grid(row=4, column=0, pady=10, sticky=W)
rqk = Entry(root, textvariable=bz, font=('黑体', 12))
rqk.grid(row=4, column=1, pady=10, columnspan=5)
bt1 = Button(root, text='提交', font=('黑体', 12))
bt1.grid(row=5, column=0, columnspan=6, padx=(20, 10), pady=10)
def w(event):
global year_var, month_var, day_var, je, lb, bz,id_var
year = year_var.get()
month = month_var.get()
day = day_var.get()
id=id_var.get()
je1 = je.get()
lb1 = lb.get()
bz1 = bz.get()
file = '记账信息.txt'
with open(file, 'a', encoding='utf-8') as file_write:
file_write.write(f"{year}.{month}.{day}.{id} ")
file_write.write(f"金额:-{je1} ")
file_write.write(f"类别:{lb1} ")
file_write.write(f"备注:{bz1}\n")
name = ''
text = '记录成功!'
messagebox.showinfo(name, text)
root.destroy()
bt1.bind('<Button-1>', w)
root.mainloop()
4.收支查询页面
from tkinter import *
from tkinter import messagebox
root = Tk()
root.title('收支记录')
root.geometry('400x250+700+300')
def filter_and_display_records(year, month):
for widget in root.winfo_children():
widget.destroy()
text_box = Text(root, height=15, width=50)
text_box.pack()
try:
with open('记账信息.txt', 'r', encoding='utf-8') as file:
for line in file:
if line.startswith(f"{year}.{month}."):
text_box.insert(END, line.strip() + '\n')
except FileNotFoundError:
messagebox.showerror("错误", "文件未找到")
except Exception as e:
messagebox.showerror("错误", str(e))
def submit():
year = year_entry.get()
month = month_entry.get()
if year and month:
filter_and_display_records(year, month)
else:
messagebox.showerror("错误", "请输入年份和月份")
year_label = Label(root, text='年份:')
year_label.pack()
year_entry = Entry(root)
year_entry.pack()
month_label = Label(root, text='月份:')
month_label.pack()
month_entry = Entry(root)
month_entry.pack()
submit_button = Button(root, text='提交', command=submit)
submit_button.pack()
root.mainloop()
5.删除页面:
import tkinter as tk
from tkinter import messagebox
def delete_record():
year = year_entry.get()
month = month_entry.get()
day = day_entry.get()
number = number_entry.get()
record_to_delete = f"{year}.{month}.{day}.{number}"
if not all([year, month, day, number]):
messagebox.showerror("错误", "请填写所有信息")
return
file_path = "记账信息.txt"
lines = []
record_found = False
with open(file_path, 'r', encoding='utf-8') as file:
for line in file:
if record_to_delete not in line:
lines.append(line)
else:
record_found = True
if record_found:
with open(file_path, 'w', encoding='utf-8') as file:
file.writelines(lines)
messagebox.showinfo("成功", "记录已删除")
root.destroy()
else:
messagebox.showinfo("信息", "未找到匹配的记录")
root = tk.Tk()
root.title('记账本')
root.geometry('500x300')
entry_frame = tk.Frame(root)
entry_frame.pack(pady=10)
tk.Label(entry_frame, text="年:").grid(row=0, column=0)
year_entry = tk.Entry(entry_frame, width=5)
year_entry.grid(row=0, column=1)
tk.Label(entry_frame, text="月:").grid(row=0, column=2)
month_entry = tk.Entry(entry_frame, width=5)
month_entry.grid(row=0, column=3)
tk.Label(entry_frame, text="日:").grid(row=0, column=4)
day_entry = tk.Entry(entry_frame, width=5)
day_entry.grid(row=0, column=5)
tk.Label(entry_frame, text="编号:").grid(row=0, column=6)
number_entry = tk.Entry(entry_frame, width=5)
number_entry.grid(row=0, column=7)
delete_button = tk.Button(root, text="删除记录", command=delete_record)
delete_button.pack(pady=10)
root.mainloop()
6.修改页面:
import tkinter as tk
from tkinter import messagebox
import subprocess
def delete_record():
year = year_entry.get()
month = month_entry.get()
day = day_entry.get()
number = number_entry.get()
record_to_delete = f"{year}.{month}.{day}.{number}"
if not all([year, month, day, number]):
messagebox.showerror("错误", "请填写所有信息")
return
file_path = "记账信息.txt"
lines = []
record_found = False
with open(file_path, 'r', encoding='utf-8') as file:
for line in file:
if record_to_delete not in line:
lines.append(line)
else:
record_found = True
if record_found:
with open(file_path, 'w', encoding='utf-8') as file:
file.writelines(lines)
name = '提示!!!'
text = '修改为收入信息正常填写,修改为支出需要在金额前加-号'
messagebox.showinfo(name, text)
subprocess.run(["python", "收入页面.py"])
root.destroy()
else:
messagebox.showinfo("信息", "未找到匹配的记录")
root = tk.Tk()
root.title('记账本')
root.geometry('500x300')
entry_frame = tk.Frame(root)
entry_frame.pack(pady=10)
tk.Label(entry_frame, text="年:").grid(row=0, column=0)
year_entry = tk.Entry(entry_frame, width=5)
year_entry.grid(row=0, column=1)
tk.Label(entry_frame, text="月:").grid(row=0, column=2)
month_entry = tk.Entry(entry_frame, width=5)
month_entry.grid(row=0, column=3)
tk.Label(entry_frame, text="日:").grid(row=0, column=4)
day_entry = tk.Entry(entry_frame, width=5)
day_entry.grid(row=0, column=5)
tk.Label(entry_frame, text="编号:").grid(row=0, column=6)
number_entry = tk.Entry(entry_frame, width=5)
number_entry.grid(row=0, column=7)
delete_button = tk.Button(root, text="修改记录", command=delete_record)
delete_button.pack(pady=10)
root.mainloop()
7.收入统计页面:
import tkinter as tk
import re
root = tk.Tk()
root.title('统计')
root.geometry('350x200')
tk.Label(root, text='年:').place(x=10, y=20)
year_entry = tk.Entry(root, width=6)
year_entry.place(x=50, y=20)
tk.Label(root, text='月:').place(x=130, y=20)
month_entry = tk.Entry(root, width=6)
month_entry.place(x=170, y=20)
tk.Label(root, text='日:').place(x=250, y=20)
day_entry = tk.Entry(root, width=6)
day_entry.place(x=290, y=20)
def qr():
year = year_entry.get()
month = month_entry.get()
day = day_entry.get()
income_sum = 0
expense_sum = 0
with open('记账信息.txt', 'r', encoding='utf-8') as file:
for line in file:
date_part = line.split(' ')[0]
match = re.search(r'金额:(-?\d+\.?\d*)', line)
if match:
amount = float(match.group(1))
if (not year or year in date_part) and \
(not month or month in date_part) and \
(not day or day in date_part):
if amount < 0:
expense_sum += amount
else:
income_sum += amount
for widget in root.winfo_children():
if isinstance(widget, tk.Entry) or isinstance(widget, tk.Button) or isinstance(widget, tk.Label):
widget.destroy()
result_text = f"收入:{income_sum:.2f}\n支出:{expense_sum:.2f}\n总计:{income_sum + expense_sum:.2f}"
result_box = tk.Text(root, height=5, width=50)
result_box.insert(tk.END, result_text)
result_box.pack()
bt1 = tk.Button(root, text='查询', command=qr)
bt1.place(x=210, y=60)
root.mainloop()
部分截图: