From 50e6da3b971444b1ee93ec62870ff256abf7481e Mon Sep 17 00:00:00 2001 From: wdmtxyh123 <2104295177@qq.com> Date: Tue, 4 Jun 2024 22:28:11 +0800 Subject: [PATCH] 123 --- ParkManage.py | 48 +++--------------------------------------------- User.py | 30 ++++++++++++++++++++++++++++++ main1.py | 43 ++++++++----------------------------------- 3 files changed, 41 insertions(+), 80 deletions(-) create mode 100644 User.py diff --git a/ParkManage.py b/ParkManage.py index 7410dd3..a74d56b 100644 --- a/ParkManage.py +++ b/ParkManage.py @@ -103,15 +103,15 @@ class ParkManage: parked_cars """ self.cursor.execute(query) - all_cars_info = self.cursor.fetchall() + car_info = self.cursor.fetchall() - if all_cars_info: + if car_info: # 遍历所有车辆信息并格式化输出 all_cars_message = "\n\n".join([ f"车牌号: {car[0]}\n" f"停放时间: {car[1].strftime('%Y-%m-%d %H:%M:%S')}\n" f"取车时间: {'未取车' if car[2] is None else car[2].strftime('%Y-%m-%d %H:%M:%S')}" - for car in all_cars_info + for car in car_info ]) messagebox.showinfo("所有车辆信息", all_cars_message) else: @@ -119,48 +119,6 @@ class ParkManage: except Error as e: messagebox.showerror("显示错误", f"显示所有车辆信息时发生错误: {e}") - def edit_car_pickup_time(self, license_plate): - """编辑车辆的取车时间""" - try: - # 查询指定车牌号的车辆是否存在 - check_query = """ - SELECT COUNT(*) - FROM parked_cars - WHERE license_plate = %s - """ - self.cursor.execute(check_query, (license_plate,)) - car_exists = self.cursor.fetchone()[0] - - if car_exists: - # 弹出对话框让用户输入新的取车时间 - new_picked_up_time_str = simpledialog.askstring("编辑取车时间", - "请输入新的取车时间(YYYY-MM-DD HH:MM:SS):", - initialvalue="2023-04-01 14:00:00") - if new_picked_up_time_str: - try: - # 尝试将输入的字符串转换为datetime对象 - from datetime import datetime - new_picked_up_time = datetime.strptime(new_picked_up_time_str, '%Y-%m-%d %H:%M:%S') - except ValueError: - messagebox.showerror("输入错误", "请输入正确的日期时间格式!") - return - - # 更新数据库中的取车时间 - update_query = """ - UPDATE parked_cars - SET picked_up_time = %s - WHERE license_plate = %s - """ - self.cursor.execute(update_query, (new_picked_up_time, license_plate)) - self.connection.commit() # 提交事务 - - messagebox.showinfo("更新成功", f"车牌号为{license_plate}的车辆取车时间已更新。") - else: - messagebox.showinfo("取消操作", "未输入取车时间,操作已取消。") - else: - messagebox.showinfo("查询结果", "未找到该车牌号的车辆信息。") - except Error as e: - messagebox.showerror("编辑错误", f"编辑车辆取车时间时发生错误: {e}") def count_parked_cars(self): """统计当前停在停车场的车辆总数""" diff --git a/User.py b/User.py new file mode 100644 index 0000000..e4dacba --- /dev/null +++ b/User.py @@ -0,0 +1,30 @@ +class User: + def __init__(self, username, password, role='user'): + """ + 初始化用户对象 + :param username: 用户名 + :param password: 密码(注意:在实际应用中不应明文存储,此处仅为示例) + :param role: 用户角色,如'user'(默认)、'admin'等 + """ + self.username = username + self.password = password # 实际应用中应加密存储 + self.role = role + + def __str__(self): + """返回用户的字符串表示""" + return f"用户名: {self.username}, 角色: {self.role}" + + def change_password(self, new_password): + """ + 更改用户密码 + :param new_password: 新密码 + """ + self.password = new_password # 实际应用中应处理密码加密逻辑 + + def promote_to_admin(self): + """提升用户为管理员角色""" + self.role = 'admin' + + def demote_to_user(self): + """降级用户为普通用户角色""" + self.role = 'user' \ No newline at end of file diff --git a/main1.py b/main1.py index 23836f2..9cb5683 100644 --- a/main1.py +++ b/main1.py @@ -73,14 +73,11 @@ def on_inquire_click(): # 假设query_car_info返回车辆信息的字典,展示相关信息 message = ( f"车牌号: {car_info['license_plate']}\n" - f"车型: {car_info['model']}\n" - f"颜色: {car_info['color']}\n" + f"停放时间: {car_info['parked_time'].strftime('%Y-%m-%d %H:%M:%S')}\n" f"取车时间: {car_info['picked_up_time'].strftime('%Y-%m-%d %H:%M:%S') if car_info['picked_up_time'] else '未取车'}" ) - messagebox.showinfo("车辆信息", message) - else: - messagebox.showerror("查询结果", "未找到该车牌号的车辆信息。") + except Exception as e: messagebox.showerror("查询错误", f"查询过程中发生错误: {e}") else: @@ -89,42 +86,19 @@ def on_display_info_click(): try: # 调用ParkManage类中的方法来获取所有车辆信息 - car_info = park.display_all_cars_info() + car_info= park.display_all_cars_info() - if cars_info: + if car_info: # 假设cars_info是一个包含车辆信息的列表,每个元素是车辆的字典表示 info_text = "\n".join( [f"车牌号: {car['license_plate']}, 停放时间: {car['parked_time'].strftime('%Y-%m-%d %H:%M:%S')}" f"{', 取车时间: ' + car['picked_up_time'].strftime('%Y-%m-%d %H:%M:%S') if car['picked_up_time'] else ''}" - for car in cars_info]) - messagebox.showinfo("车辆信息", info_text) - else: - messagebox.showinfo("信息提示", "当前没有车辆信息可显示。") + for car in car_info]) + except Exception as e: messagebox.showerror("错误", f"显示车辆信息时发生错误: {e}") -def on_edit_info_click(): - - # 从界面获取车牌号、车型和颜色 - license_plate = license_plate_entry.get().strip() - new_model = model_entry.get().strip() - new_color = color_entry.get().strip() - - if not license_plate: - messagebox.showerror("错误", "请输入车牌号进行编辑!") - return - - try: - # 假设ParkManage类有一个edit_car_info方法用于更新车辆信息 - success = park_manage_instance.edit_car_info(license_plate, new_model=new_model, new_color=new_color) - - if success: - messagebox.showinfo("成功", "车辆信息已成功更新。") - else: - messagebox.showinfo("失败", "未能找到或更新车辆信息,请检查车牌号是否正确。") - except Exception as e: - messagebox.showerror("错误", f"编辑车辆信息时发生错误: {e}") def on_statistics_click(): @@ -163,7 +137,7 @@ def on_exit_click(): def refresh_ui(): - """根据当前模式刷新界面""" + # 在此函数中移除与编辑车辆信息按钮相关的代码 for widget in window.winfo_children(): widget.destroy() @@ -176,9 +150,9 @@ def refresh_ui(): ] if isAdmin: + # 从这里移除了与编辑车辆信息相关的按钮 additional_buttons = [ ("显示车辆信息", on_display_info_click), - ("编辑车辆信息", on_edit_info_click), ("统计信息", on_statistics_click), ("切换至用户模式", on_switch_mode), ] @@ -191,7 +165,6 @@ def refresh_ui(): for idx, (text, cmd) in enumerate(buttons): tk.Button(window, text=text, command=cmd).pack(fill=tk.X, pady=5) - def main_gui(): global window window = tk.Tk()