暑假每天都要填表算账很烦躁,就整了个小程序来减轻压力
程序可以做到记录输入的每一条数据,并用新数据减去旧数据算新增的量,同时记录填写时间
Python代码
import json
import os # 导入os模块
from datetime import datetime
from tkinter import *
from tkinter import messagebox
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
from matplotlib.figure import Figure
import matplotlib.pyplot as plt
# 初始化数据文件(如果文件不存在,则创建空字典)
def init_data_file(filename):
if not os.path.exists(filename):
with open(filename, 'w') as file:
json.dump({}, file)
# 读取数据
def read_data(filename):
with open(filename, 'r') as file:
return json.load(file)
# 更新数据
def update_data(filename, data_name, new_value):
data = read_data(filename)
current_time = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
if data_name not in data:
data[data_name] = [
{"time": current_time, "value": new_value, "difference": 0}
]
else:
last_record = data[data_name][-1]
last_value = last_record["value"]
difference = new_value - last_value
data[data_name].append({
"time": current_time,
"value": new_value,
"difference": difference
})
with open(filename, 'w') as file:
json.dump(data, file, indent=4)
# 主程序
# GUI主窗口
class DataApp:
def __init__(self, master):
self.master = master
self.master.title("数据记录器")
self.filename = 'data_records.json'
self.init_data_file()
# 创建输入框和按钮
Label(master, text="数据名称:").grid(row=0)
self.data_name_entry = Entry(master)
self.data_name_entry.grid(row=0, column=1)
Label(master, text="数据值:").grid(row=1)
self.data_value_entry = Entry(master)
self.data_value_entry.grid(row=1, column=1)
self.update_button = Button(master, text="更新数据", command=self.update_data_gui)
self.update_button.grid(row=2, column=1)
self.analyze_button = Button(master, text="分析数据", command=self.analyze_data)
self.analyze_button.grid(row=3, column=1)
self.clear_button = Button(master, text="清空数据库", command=self.clear_data)
self.clear_button.grid(row=6, column=1)
self.quit_button = Button(master, text="退出", command=master.quit)
self.quit_button.grid(row=4, column=1)
# 图表区域
self.figure = Figure(figsize=(6, 4), dpi=100)
self.ax = self.figure.add_subplot(111)
self.canvas = FigureCanvasTkAgg(self.figure, master=master)
self.canvas.get_tk_widget().grid(row=0, column=2, rowspan=5)
self.quit_button = Button(master, text="图表退出", command=master.quit)
self.quit_button.grid(row=5, column=1)
# 数据清空功能
def clear_data(self):
confirm = messagebox.askyesno("警告", "您确定要清空所有数据吗?此操作不可逆!")
if confirm:
with open(self.filename, 'w') as file:
json.dump({}, file)
messagebox.showinfo("成功", "数据已清空。")
else:
messagebox.showinfo("取消", "数据清空操作已取消。")
# 初始化数据文件
def init_data_file(self):
if not os.path.exists(self.filename):
with open(self.filename, 'w') as file:
json.dump({}, file)
# GUI版本的更新数据函数
def update_data_gui(self):
data_name = self.data_name_entry.get()
try:
new_value = float(self.data_value_entry.get())
update_data(self.filename, data_name, new_value)
messagebox.showinfo("成功", f"数据 {data_name} 已更新,并记录了差值和时间。")
except ValueError:
messagebox.showerror("错误", "请输入有效的数字!")
# 数据分析图表函数
def analyze_data(self):
data_name = self.data_name_entry.get()
data = read_data(self.filename)
if data_name in data:
times = [record["time"] for record in data[data_name]]
values = [record["value"] for record in data[data_name]]
differences = [record["difference"] for record in data[data_name]]
self.ax.clear()
self.ax.plot(times, values, label='Values', marker='o') # 添加marker样式以便更清晰地看到点
self.ax.plot(times, differences, label='Differences', marker='x')
# 在每个点旁边添加数值标签
for i, txt in enumerate(values):
self.ax.text(times[i], values[i], f'{txt:.2f}', ha='center', va='bottom')
for i, txt in enumerate(differences):
self.ax.text(times[i], differences[i], f'{txt:.2f}', ha='center', va='top')
self.ax.legend()
self.ax.set_title(f"{data_name} Analysis")
self.canvas.draw()
else:
messagebox.showerror("错误", f"数据名称 {data_name} 未找到!")
if __name__ == "__main__":
root = Tk()
app = DataApp(root)
root.mainloop()
Python运行结果
查询数据
exe也做了一个
链接:https://pan.baidu.com/s/1YUkjP5Vs76tU9tzRmh_kZw?pwd=1111
提取码:1111
过期或者下架了评论区提醒我补下。