import sqlite3 import tkinter as tk import tkinter.messagebox as msgbox import json import subprocess window = tk.Tk() window.resizable(0, 0) window.title("停车场信息管理系统") # 窗口居中 ww, wh = 400, 200 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)) # 标签提示 l_info = tk.Label(window, text="请输入想要查询的内容:", font=("宋体", 16)) l_info.pack(padx=20, pady=10, anchor="nw") # 单行文本框输入信息 var = tk.StringVar() e_info = tk.Entry(window, width=40, textvariable=var) e_info.place(x=50, y=60) # 单选框让用户自己选择查找的数据类型 v = tk.IntVar() v.set(1) rb1 = tk.Radiobutton(window, text="ID", variable=v, value=1, takefocus=True) rb2 = tk.Radiobutton(window, text="NAME", variable=v, value=2, takefocus=True) rb3 = tk.Radiobutton(window, text="PHONE", variable=v, value=3, takefocus=True) rb1.place(x=20, y=100) rb2.place(x=60, y=100) rb3.place(x=130, y=100) # 检查和覆写临时存档文件(保存查找到的信息的id) def check_or_save(satisfied_id): if len(satisfied_id) == 0: msgbox.showinfo(message="查询失败,请检查输入信息") else: # 将数据写入临时存档文件sq_record中 with open("sq_record.json", "w+") as fileId: temp_list = list(satisfied_id) json.dump(temp_list, fileId) # 连接新窗口 subprocess.run(["python", "results_found.py"], check=True) def collect_ids(info, column_name, satisfied_id, cur): """搜集符合条件的id""" cur.execute(f"select id from car where {column_name} like ?", ('%' + info + '%',)) try: while True: satisfied_id.add(cur.fetchone()[0]) except TypeError: msgbox.showinfo(message="搜索完毕") # 查找 def select_info(): # 获得输入框信息 info = var.get() # 创建一个集合记录符合条件的id satisfied_id = set() # 检查输入框是否为空 if info == "": msgbox.showerror(message="输入框为空") else: # 连接数据库 con = sqlite3.connect("./car_info.db") cur = con.cursor() # 获得单选框的值,选择匹配类目,并匹配要查询的内容 if v.get() == 1: collect_ids(info, "id", satisfied_id, cur) check_or_save(satisfied_id) elif v.get() == 2: collect_ids(info, "name", satisfied_id, cur) check_or_save(satisfied_id) elif v.get() == 3: collect_ids(info, "phone", satisfied_id, cur) check_or_save(satisfied_id) cur.close() con.close() btn_select = tk.Button(window, text="查询", width=10, command=select_info) btn_exit = tk.Button(window, text="退出", width=10, command=window.quit) btn_select.place(x=100, y=150) btn_exit.place(x=200, y=150) window.mainloop()