You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

178 lines
6.0 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

import tkinter as tk
from tkinter import messagebox
from tkinter import simpledialog
from Car import Car
from ParkManage import ParkManage # 确保你的模块路径正确
from User import User
# 初始化停车场管理
park_manage_instance = ParkManage()
park = ParkManage()
# 创建一个用户实例
user1 = User('Alice', 'P@ssw0rd')
# 打印用户信息
print(user1)
# 修改用户密码
user1.change_password('NewP@ssw0rd')
# 提升用户为管理员
user1.promote_to_admin()
# 再次打印用户信息,查看角色是否变化
print(user1)
# 降级用户为普通用户
user1.demote_to_user()
# 最后一次打印用户信息
print(user1)
isAdmin = False
def on_park_click():
"""处理停车操作的回调函数"""
# 弹出对话框让用户输入车牌号
license_plate = simpledialog.askstring("停车", "请输入车牌号:")
if license_plate:
# 调用park对象的park方法传入用户输入的车牌号
success = park.park(license_plate)
if success:
messagebox.showinfo("停车成功", f"车牌号{license_plate}的车辆已成功停放。")
else:
messagebox.showerror("停车失败", "停车过程中出现问题,请重试。")
else:
messagebox.showwarning("警告", "车牌号不能为空,请重新输入。")
def on_pickup_click():
"""处理取车操作的回调函数"""
# 弹出对话框让用户输入车牌号
license_plate = simpledialog.askstring("取车", "请输入车牌号:")
if license_plate:
# 调用park对象的pickup方法传入用户输入的车牌号尝试取车
success = park.pickup(license_plate)
if success:
messagebox.showinfo("取车成功", f"车牌号为{license_plate}的车辆已成功取走。")
else:
messagebox.showerror("取车失败", f"车牌号为{license_plate}的车辆取车失败。请检查车牌号是否正确或车辆是否已取走。")
else:
messagebox.showwarning("警告", "车牌号不能为空,请重新输入。")
def on_inquire_click():
"""处理查询车辆信息操作的回调函数"""
# 弹出对话框让用户输入车牌号
license_plate = simpledialog.askstring("查询车辆信息", "请输入车牌号:")
if license_plate:
try:
# 调用park对象的query_car_info方法传入用户输入的车牌号查询车辆信息
car_info = park.query_car_info(license_plate)
if car_info:
# 假设query_car_info返回车辆信息的字典展示相关信息
message = (
f"车牌号: {car_info['license_plate']}\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 '未取车'}"
)
except Exception as e:
messagebox.showerror("查询错误", f"查询过程中发生错误: {e}")
else:
messagebox.showwarning("警告", "车牌号不能为空,请重新输入。")
def on_display_info_click():
try:
# 调用ParkManage类中的方法来获取所有车辆信息
car_info= park.display_all_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 car_info])
except Exception as e:
messagebox.showerror("错误", f"显示车辆信息时发生错误: {e}")
def on_statistics_click():
try:
# 假设ParkManage类中包含以下统计方法
parked_cars_count = park_manage_instance.count_parked_cars()
empty_slots = park_manage_instance.count_empty_slots(total_slots=100) # 假设总车位数为100根据实际情况调整
most_frequent_time = park_manage_instance.most_frequent_parking_time()
# 构建统计信息字符串
stats_message = (
f"当前停车数量: {parked_cars_count}\n"
f"空闲车位数: {empty_slots}\n"
f"最频繁的停车时段: {most_frequent_time}\n"
)
messagebox.showinfo("统计信息", stats_message)
except Exception as e:
messagebox.showerror("错误", f"统计车辆信息时发生错误: {e}")
def on_switch_mode():
"""处理模式切换的回调函数"""
global isAdmin
isAdmin = not isAdmin
refresh_ui()
def on_exit_click():
"""处理退出操作的回调函数"""
if messagebox.askyesno("退出", "确定要退出吗?"):
window.destroy() # 关闭窗口
def refresh_ui():
# 在此函数中移除与编辑车辆信息按钮相关的代码
for widget in window.winfo_children():
widget.destroy()
tk.Label(window, text=f"欢迎使用停车场管理系统 - {'管理员' if isAdmin else '用户'}模式").pack()
buttons = [
("停车", on_park_click),
("取车", on_pickup_click),
("查询车辆信息", on_inquire_click),
]
if isAdmin:
# 从这里移除了与编辑车辆信息相关的按钮
additional_buttons = [
("显示车辆信息", on_display_info_click),
("统计信息", on_statistics_click),
("切换至用户模式", on_switch_mode),
]
buttons.extend(additional_buttons)
else:
buttons.append(("切换至管理员模式", on_switch_mode))
buttons.append(("退出", on_exit_click))
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()
window.title("停车场管理系统")
refresh_ui() # 初始化界面
window.mainloop()
if __name__ == "__main__":
main_gui()