import os标签:box,03,2024.06,10,text,zb,jzr,row From: https://www.cnblogs.com/dmx-03/p/18243904
import openpyxl
import datetime
from tkinter import *
from tkinter import messagebox
# Create the main window
jzr = Tk()
jzr.title('个人账本记录')
jzr.geometry('400x600')
jzr.config(bg='light yellow')
# Create a text box to display records
text_box = Text(jzr, font=('微软雅黑', 10), width=40, height=20)
text_box.grid(row=9, column=0, columnspan=2, padx=10, pady=10)
# Function to record expenses
def zcjl():
filepath = '个人账本.xlsx'
if os.path.exists(filepath):
zb = openpyxl.load_workbook(filepath)
else:
zb = openpyxl.Workbook()
zb.active['A1'] = '日期'
zb.active['B1'] = '类型'
zb.active['C1'] = '金额'
xf = zb.active
a = datetime.datetime.now()
b = Entry5.get()
c = Entry6.get()
xf.append([a, b, -float(c)]) # Save expense as negative value
zb.save(filepath)
messagebox.showinfo('提示', message='支出数据已记录完成')
# Clear text box content
text_box.delete('1.0', END)
# Display the expense record in the text box
text_box.insert(END, f'日期:{a}\n类型:{b}\n金额:{c}\n\n')
def srjl():
filepath = '个人账本.xlsx'
if os.path.exists(filepath):
zb = openpyxl.load_workbook(filepath)
else:
zb = openpyxl.Workbook()
zb.active['A1'] = '日期'
zb.active['B1'] = '类型'
zb.active['C1'] = '金额'
xf = zb.active
a = datetime.datetime.now()
b = Entry7.get()
c = Entry8.get()
xf.append([a, b, float(c)]) # Save income as positive value
zb.save(filepath)
messagebox.showinfo('提示', message='收入数据已记录完成')
# Clear text box content
text_box.delete('1.0', END)
# Display the income record in the text box
text_box.insert(END, f'日期:{a}\n类型:{b}\n金额:{c}\n\n')
# Create labels, entry fields, and button for expense tracking
label5 = Label(jzr, text='支出类型:', font=('微软雅黑', 10), fg='red')
label5.grid(row=4, column=0, padx=5, pady=5)
label6 = Label(jzr, text='支出金额:', font=('微软雅黑', 10), fg='red')
label6.grid(row=5, column=0, padx=5, pady=5)
Entry5 = Entry(jzr, font=('微软雅黑', 12), width=16)
Entry5.grid(row=4, column=1, padx=5, pady=5)
Entry6 = Entry(jzr, font=('微软雅黑', 12), width=16)
Entry6.grid(row=5, column=1, padx=5, pady=5)
# Button to record expenses
Button4 = Button(jzr, text='记录支出', font=('微软雅黑', 10), width=8, command=zcjl)
Button4.grid(row=6, column=0, columnspan=2, pady=10)
# Create labels, entry fields, and button for income tracking
label7 = Label(jzr, text='收入类型:', font=('微软雅黑', 10), fg='green')
label7.grid(row=10, column=0, padx=5, pady=5)
label8 = Label(jzr, text='收入金额:', font=('微软雅黑', 10), fg='green')
label8.grid(row=11, column=0, padx=5, pady=5)
Entry7 = Entry(jzr, font=('微软雅黑', 12), width=16)
Entry7.grid(row=10, column=1, padx=5, pady=5)
Entry8 = Entry(jzr, font=('微软雅黑', 12), width=16)
Entry8.grid(row=11, column=1, padx=5, pady=5)
# Button to record income
Button7 = Button(jzr, text='记录收入', font=('微软雅黑', 10), width=8, command=srjl)
Button7.grid(row=12, column=0, columnspan=2, pady=10)
# Create a button to query records
def query_records():
text_box.delete('1.0', END)
filepath = '个人账本.xlsx'
if os.path.exists(filepath):
zb = openpyxl.load_workbook(filepath)
xf = zb.active
for row in xf.iter_rows(min_row=2, values_only=True):
text_box.insert(END, f'日期:{row[0]}\n类型:{row[1]}\n金额:{row[2]}\n\n')
text_box.insert(END, '------------------------------------------\n')
else:
messagebox.showinfo('提示', message='暂无记录')
Button5 = Button(jzr, text='查询记录', font=('微软雅黑', 10), width=8, command=query_records)
Button5.grid(row=7, column=0, columnspan=2, pady=10)
# Calculate total income, expenses, and balance
def calculate_total():
total_income = 0
total_expenses = 0
filepath = '个人账本.xlsx'
if os.path.exists(filepath):
zb = openpyxl.load_workbook(filepath)
xf = zb.active
for row in xf.iter_rows(min_row=2, values_only=True):
if isinstance(row[2], int) or isinstance(row[2], float):
if row[2] > 0:
total_income += row[2]
else:
total_expenses += row[2]
balance = total_income + total_expenses
messagebox.showinfo('总收入支出', message=f'总收入: {total_income}\n总支出: {abs(total_expenses)}\n结余: {balance}')
Button6 = Button(jzr, text='计算总额', font=('微软雅黑', 10), width=8, command=calculate_total)
Button6.grid(row=8, column=0, columnspan=2, pady=10)
# Run the main loop
jzr.mainloop()