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()