From 6ff95f6822a65fdfedf58b013415f17c435a1c24 Mon Sep 17 00:00:00 2001 From: p85pylmu6 <2315457363@qq.com> Date: Fri, 31 May 2024 15:27:18 +0800 Subject: [PATCH] ADD file via upload --- results_found.py | 137 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 137 insertions(+) create mode 100644 results_found.py diff --git a/results_found.py b/results_found.py new file mode 100644 index 0000000..3627b6a --- /dev/null +++ b/results_found.py @@ -0,0 +1,137 @@ +import sqlite3 +import json +import tkinter as tk +import tkinter.messagebox as msgbox +import datetime + +# 获得sq_record中的序号sq,根据序号数量确定页码 +with open("sq_record.json", "r") as file: + sequence = json.load(file) + +window = tk.Tk() +window.resizable(1, 0) +window.title("查询结果") +# 窗口居中偏右 +ww, wh = 800, 400 +sw, sh = window.winfo_screenwidth(), window.winfo_screenheight() +x, y = (sw - ww) / 2, (sh - wh) / 2 +window.geometry("%dx%d+%d+%d" % (ww, wh, x, y)) + +# 搭建存储文本框的frame +f1 = tk.Frame(window, width=ww - 20, height=wh - 70, bg="lightpink") +f1.pack(padx=10, pady=10) +# 创建多行文本框 +t1 = tk.Text(f1, width=ww - 40, height=21) +t1.pack(side="top", padx=10, pady=10) + +lb = tk.Label(window, text="请输入想要出场的车牌号:") +lb.pack(side="left", padx=10, pady=10, anchor="sw") +var_num = tk.StringVar() +e1 = tk.Entry(window, textvariable=var_num) +e1.pack(side="left", padx=10, pady=10, anchor="sw") + + +def showing(): + """显示所有indexs中的成员信息""" + t1.delete(1.0, tk.END) + t1.insert(1.0, "车牌号\t\t姓名\t\t颜色\t\t住址\t\t电话\t\t停车时长\n") + con = sqlite3.connect("car_info.db") + cur = con.cursor() + for i in sequence: + cur.execute("select * from car where id=?", (i,)) + res = cur.fetchone() + try: + # 显示停车时长 + stay_hours_str = res[5] # 获取 stay_hours 作为字符串 + if stay_hours_str == 0: + stay_hours = "未停车" + else: + # 将 stay_hours 字符串解析为 datetime 对象 + entry_time = datetime.datetime.strptime(stay_hours_str, "%Y-%m-%d %H:%M:%S") + + # 计算入场时间和当前时间的时间差 + current_time = datetime.datetime.now() + stay_hours = (current_time - entry_time).total_seconds() / 3600 + + # 格式化 stay_hours 以便显示 + stay_hours = f"{stay_hours:.2f} 小时" + + t1.insert("end", "{}\t\t{}\t\t{}\t\t{}\t\t{}\t\t{}\n" + .format(res[0], res[1], res[2], res[3], res[4], stay_hours)) + except TypeError: + pass + # 关闭数据库 + cur.close() + con.close() + +#计算停车时长 +def delete(temp): + con = sqlite3.connect("car_info.db") + cur = con.cursor() + + # 获取当前时间 + current_time = datetime.datetime.now() + + # 获取数据库中的入场时间 + cur.execute("SELECT stay_hours FROM car WHERE id = ?", (temp,)) + entry_time_str = cur.fetchone()[0] + + # 如果 stay_hours 字段为空,则使用当前时间作为入场时间 + if entry_time_str == 0: + entry_time_str = current_time.strftime("%Y-%m-%d %H:%M:%S") + + # 解析时间字符串 + entry_time = datetime.datetime.strptime(entry_time_str, "%Y-%m-%d %H:%M:%S") + + # 计算停车时长 + stay_hours = (current_time - entry_time).total_seconds() / 3600 + + # 更新 stay_hours 字段 + cur.execute("UPDATE car SET stay_hours = ? WHERE id = ?", (stay_hours, temp)) + con.commit() + + # 删除车辆信息 + cur.execute("delete from car where id=?", (str(temp),)) + + cur.execute("select id from car") + with open("sq_record.json", "w+") as fp: + indexList = cur.fetchall() + dumpList = [i[0] for i in indexList] + json.dump(dumpList, fp) + + con.commit() + cur.close() + con.close() + # 返回 stay_hours + return stay_hours + +#车辆出场,计算停车费 +def del_member(): + temp = var_num.get() + # 检测输入框 + if temp == "": + msgbox.showerror(message="输入框为空") + elif temp in sequence: + stay_hours = delete(temp) # 获取 stay_hours + + # 计算停车费 + parking_fee = stay_hours * 5 # 假设每小时收费 5 元 + + # 输出停车费 + msgbox.showinfo("停车费", f"停车时长: {stay_hours:.2f} 小时\n停车费: {parking_fee:.1f} 元") + + msgbox.showinfo(message="车辆出场成功!") + showing() + else: + msgbox.showerror(title="error", message="删除失败,请检查输入的Id") + + +btn_exit = tk.Button(window, text="退出", command=window.destroy) +btn_del = tk.Button(window, text="车辆出场", command=del_member) +# 放置 +btn_exit.pack(side="right", padx=10, pady=10, anchor="se") +btn_del.pack(side="right", pady=10, anchor="se") + +showing() + +window.mainloop() \ No newline at end of file