1、系统分析
1.1 项目背景
随着传统宿舍管理方式的不足和现代化管理需求的日益增长,以及信息技术的发展和智能化设备的普及,学生宿舍管理系统应运而生。该系统利用先进的技术手段,实现对宿舍内设施设备、学生出入管理、安全监控等方面的智能化管理,提升管理效率和服务质量。同时,学生宿舍系统设计也考虑到了学生的个性化需求和隐私保护,通过设置个性化服务功能和严格的数据保护措施,既满足了学生的个性化需求,又保障了信息安全。
总的来说,学生宿舍系统的设计是为了提升宿舍管理的现代化水平,满足学生和管理者对高效、便捷、安全管理的需求,为学生提供一个舒适、安全的居住环境,促进学生的学习和生活质量的提升。
1.2可行性分析
1.2.1技术可行性
宿舍管理系统的需求明确且具体,包括宿舍申请、分配、入住、退宿、维修、考勤等各个方面的功能。本系统利用Mysql进行数据库的建立,利用python进行界面的实现。 随着技术的不断发展和用户需求的不断变化,宿舍管理系统也需要不断更新和迭代。因此,系统应具备良好的技术更新和迭代能力,以便及时修复漏洞、添加新功能并优化性能。本系统的各逻辑结构清晰,系统操作简单直观,便于对系统进行升级和改善。
1.2.2经济可行性
本项目开发不需要额外的开支,投资成本较小。同时,本项目还具有良好的市场前景,争对大环境下对于教育及其质量的重视,公寓管理是必不可少的部分,通过该管理系统,学校可以更好的管理学生的住宿生活,提高安全性,提升工作效率。
1.3需求分析
需求分析:
1. **学生信息管理:**
- 需要记录学生的学号(sno)、姓名(sname)、专业(major)、班级(class)、性别(sex)、所属楼栋(building)、房间号(room)、联系方式(contact_information)、年龄(age)、密码(code)、床位(bed)等信息。
- 每个学生的学号是唯一的,作为主键(PRIMARY KEY)。
- 这张表用于管理宿舍中的学生信息,包括学生的个人信息以及所居住的位置信息。
2. **管理员信息管理:**
- 记录管理员的编号(msno)、姓名(mname)、性别(sex)、年龄(age)、所管理的楼栋(building)、联系方式(contact_information)、密码(code)等信息。
- 每个管理员的编号是唯一的,作为主键(PRIMARY KEY)。
- 这张表用于管理宿舍管理员的信息,包括管理员的个人信息以及管理的楼栋信息。
3. **宿舍楼信息管理:**
- 记录宿舍楼的楼栋编号(building)、宿舍管理员编号(manager_msno)、宿舍管理员姓名(manager_mname)、分类(classification,如男生宿舍、女生宿舍等)、楼层数(floor)等信息。
- 每个楼栋的楼栋编号是唯一的,作为主键(PRIMARY KEY)。
- 这张表用于管理宿舍楼的信息,包括宿舍楼的基本信息以及宿舍管理员的分配情况。
4. **宿舍房间信息管理:**
- 记录每个宿舍房间所在楼栋(building)、房间号(room)、房间可容纳人数(size)、舍长(supervisor)、舍长联系方式(contact_supervisor)、是否已住满(is_full)、剩余床位数(spare)等信息。
- 每个房间的房间号和所在楼栋组合起来是唯一的,作为主键(PRIMARY KEY)。
- 这张表用于管理宿舍房间的信息,包括房间的基本信息以及房间的住宿情况。
5. **宿舍信息总表管理:**
- 记录每个宿舍的楼栋(building)、房间号(room)、日期(date)、维护情况(maintain)、违规情况(violation)、违规床位(violation_bed)、能源记录(energy_record)等信息。
- 这张表用于综合管理宿舍的信息,包括宿舍的维护记录、违规情况以及能源消耗记录等。
6. **卫生情况管理:**
- 记录每个宿舍的楼栋(building)、房间号(room)、床位编号(bed)、日期(date)、个人卫生评分(personal_score)、泳池卫生评分(pool_score)、厕所卫生评分(toilet_score)、阳台卫生评分(balcony_score)、总体卫生评分(total_score)等信息。
- 这张表用于管理宿舍的卫生情况,包括各项卫生评分以及总体卫生评分。
7. **维修情况管理:**
- 记录每个宿舍的楼栋(building)、房间号(room)、日期(date)、维修原因(reason)、维修人员编号(maintain_man_sno)、维修人员姓名(maintain_man_sname)、维修人员联系方式(maintain_man_contact)、维修费用(cost)等信息。
- 这张表用于管理宿舍的维修情况,包括维修记录以及维修费用。
1.4 数据字典
1. **学生信息表 (student)**
- sno: 字符串,长度不超过20,学号 (主键)
- sname: 字符串,长度不超过50,姓名
- major: 字符串,长度不超过50,专业
- class: 字符串,长度不超过50,班级
- sex: 字符串,长度不超过10,性别
- building: 字符串,长度不超过50,所在楼栋
- room: 字符串,长度不超过10,所在房间号
- contact_information: 字符串,长度不超过100,联系方式
- age: 整数,年龄
- code: 字符串,长度不超过20,密码
- bed: 字符串,长度不超过10,床位
2. **管理员信息表 (manager)**
- msno: 字符串,长度不超过20,管理员编号 (主键)
- mname: 字符串,长度不超过50,姓名
- sex: 字符串,长度不超过10,性别
- age: 整数,年龄
- building: 字符串,长度不超过50,管理楼栋
- contact_information: 字符串,长度不超过100,联系方式
- code: 字符串,长度不超过20,密码
3. **宿舍楼信息表 (dormitory_building)**
- building: 字符串,长度不超过50,楼栋 (主键)
- manager_msno: 字符串,长度不超过20,宿舍管理员编号
- manager_mname: 字符串,长度不超过50,姓名
- classification: 字符串,长度不超过10,分类 (男生/女生)
- floor: 整数,楼层数
4. **宿舍房间信息表 (dormitory_room)**
- building: 字符串,长度不超过50,所在楼栋
- room: 字符串,长度不超过10,房间号
- size: 整数,几人间
- supervisor: 字符串,长度不超过50,舍长
- contact_supervisor: 字符串,长度不超过15,舍长联系方式
- is_full: 布尔值,是否住满
- spare: 整数,剩余床位数
5. **宿舍信息总表 (information)**
- building: 字符串,长度不超过255,楼栋
- room: 整数,房间号
- date: 日期
- maintain: 文本,维修信息
- violation: 文本,违规信息
- violation_bed: 字符串,长度不超过255,违规床位
- energy_record: 十进制,10位整数,2位小数,能源记录
6. **卫生检查表 (hygiene)**
- building: 字符串,长度不超过255,楼栋
- room: 整数,房间号
- bed: 字符串,长度不超过255,床位
- date: 日期
- personal_score: 十进制,3位整数,1位小数,个人得分
- pool_score: 十进制,3位整数,1位小数,游泳池得分
- toilet_score: 十进制,3位整数,1位小数,卫生间得分
- balcony_score: 十进制,3位整数,1位小数,阳台得分
- total_score: 十进制,3位整数,1位小数,总分
7. **维修表 (maintain)**
- building: 字符串,长度不超过255,楼栋
- room: 整数,房间号
- date: 日期
- reason: 文本,维修原因
- maintain_man_sno: 字符串,长度不超过255,维修工人编号
- maintain_man_sname: 字符串,长度不超过255,维修工人姓名
- maintain_man_contact: 字符串,长度不超过255,维修工人联系方式
- cost: 十进制,10位整数,2位小数,维修费用
8. **维修工人表 (maintain_man)**
- maintain_man_sno: 字符串,长度不超过255,维修工人编号 (主键)
- maintain_man_sname: 字符串,长度不超过255,维修工人姓名
- free: 布尔值,默认为TRUE,表示是否空闲
- maintain_man_contact: 字符串,长度不超过255,维修工人联系方式
1.5 数据流(DFD图)
2、系统设计
2.1 系统功能设计
2.1.1实体关系
学生与房间:多对一关系,一个房间可以住多个学生,但一个学生只能住一个房间。
房间与电费:一对一关系,每个房间对应一个电费记录。
宿舍管理员与楼栋:多对一关系,一个宿舍管理员可以管理多个楼栋,但一个楼栋只能由一个宿舍管理员管理(或可以是多个管理员共同管理一个楼栋,具体取决于实际业务需求)。
房间与卫生检查记录、违规记录:一对多关系,一个房间可以有多个卫生检查记录和违规记录。
2.1.2数据完整性
主键约束:每个实体都需要一个唯一的主键,如学生的学号、房间的楼栋号和房间号组合、管理员的工号等。
外键约束:在相关表之间建立外键约束,以确保数据的引用完整性。
2.1.3 性能考虑
索引:对经常用于查询的字段建立索引,以提高查询性能。
分区:如果数据量非常大,可以考虑对表进行分区,以提高查询和管理效率。
2.1.4安全性
权限管理:确保只有授权的用户才能访问和修改数据库中的数据。
数据备份:定期备份数据库,以防止数据丢失或损坏。
2.1.5学生公寓功能表
2.2 数据库设计
2.2.1 数据库需求分析
1.学生实体,包括生源地,姓名,性别,班级,学号,入学年份,年制,系部,专业,楼栋号,房间号,入住时间等属性
2.房间实体,包括楼栋号,房间号,可住人数,入住人数,寝费标准等属性
3.电费实体,包括楼栋号,房间号,现有度数,本月使用度数,上月使用度数等属性
4.宿舍管理员实体,包括姓名,性别,工号,楼栋号等属性
5.卫生检查记录实体,包括检查日期,楼栋号,房间号,分数等属性
6.违规记录实体,包括违规宿舍,检查日期,违规原因,处分等属性
2.2.2 数据库概念结构设计
1.局部E-R图
2.总体E-R图
2.2.3 数据库逻辑结构设计
1. 功能模块
学生管理模块
(1)
学生信息登记
学生信息查询
学生信息更新
宿舍管理模块
(2)
宿舍信息登记
宿舍信息查询
宿舍信息更新
入住管理模块
(3)
学生入住登记
学生退宿登记
宿舍分配和调整
宿舍维护模块
(4)
宿舍维修申请
维修进度跟踪
维修记录查询
统计与报表模块
(5)
宿舍入住率统计
维修情况统计
各类报表生成
2. 数据库设计
(1)表名:student
属性:
- sno: VARCHAR(20)
- sname: VARCHAR(50)
- major: VARCHAR(50)
- class: VARCHAR(50)
- sex: VARCHAR(10)
- building: VARCHAR(50)
- room: VARCHAR(10)
- contact_information: VARCHAR(100)
- age: INT
- code: VARCHAR(20)
- bed: VARCHAR(10)
主键:sno
(2)表名:manager
属性:
- msno: VARCHAR(20)
- mname: VARCHAR(50)
- sex: VARCHAR(10)
- age: INT
- building: VARCHAR(50)
- contact_information: VARCHAR(100)
- code: VARCHAR(20)
主键:msno
(3)表名:dormitory_building
属性:
- building: VARCHAR(50)
- manager_msno: VARCHAR(20)
- manager_mname: VARCHAR(50)
- classification: VARCHAR(10)
- floor: INT
主键:building
(4)表名:dormitory_room
属性:
- building: VARCHAR(50)
- room: VARCHAR(10)
- size: INT
- supervisor: VARCHAR(50)
- contact_supervisor: VARCHAR(15)
- is_full: BOOLEAN
- spare: INT
主键:building, room
(5)表名:information
属性:
- building: VARCHAR(255)
- room: INT
- date: DATE
- maintain: TEXT
- violation: TEXT
- violation_bed: VARCHAR(255)
- energy_record: DECIMAL(10, 2)
主键:(building, room, date)
(6)表名:hygiene
属性:
- building: VARCHAR(255)
- room: INT
- bed: VARCHAR(255)
- date: DATE
- personal_score: DECIMAL(3, 1)
- pool_score: DECIMAL(3, 1)
- toilet_score: DECIMAL(3, 1)
- balcony_score: DECIMAL(3, 1)
- total_score: DECIMAL(3, 1)
主键:(building, room, bed, date)
(7)表名:maintain
属性:
- building: VARCHAR(255)
- room: INT
- date: DATE
- reason: TEXT
- maintain_man_sno: VARCHAR(255)
- maintain_man_sname: VARCHAR(255)
- maintain_man_contact: VARCHAR(255)
- cost: DECIMAL(10, 2)
主键:(building, room, date)
(8)表名:maintain_man
属性:
- maintain_man_sno: VARCHAR(255)
- maintain_man_sname: VARCHAR(255)
- free: BOOLEAN
- maintain_man_contact: VARCHAR(255)
主键:maintain_man_sno
2.2.4 数据库物理结构设计
1.存取方法-索引建立
(1)为dormitory_room表上(building,room)建立索引,方便快速查找到房间号。
(2)为student 表的(sno,bulding,room)字段建立索引,用于快速查找学生和他们的宿舍。
2.设计关系
(1)电费表中的通过字段(buildingno,roomno)与Room表关联
(2)管理员表通过字段(buildingno)与Room表关联
(3)学生表通过字段(buildingni)与管理员表关联
2.评价数据库物理结构
(1)时间效率:
宿舍管理系统在处理用户请求方面表现出色。系统的查询和响应时间非常快速,用户可以快速获取学生信息、分配宿舍以及处理报修请求。批处理任务的效率也很高,系统能够迅速生成各种报表并进行数据导入导出操作。总体而言,宿舍管理系统在时间效率方面表现出了高水平。
(2)空间效率:
系统对数据的存储利用率很高,采用了有效的数据库设计和索引优化策略。这不仅减少了数据存储空间的占用,还提高了查询效率。此外,系统界面设计简洁明了,用户可以轻松找到需要的信息和功能,减少了操作所需的空间和时间。
(3)维护成本:
宿舍管理系统的维护成本相对较低。系统所需的硬件设备具有良好的可靠性和易于维护的特点,减少了硬件维护成本。软件方面,系统及时更新并得到持续的支持,保证了系统的安全性和性能。综合考虑,宿舍管理系统的维护成本控制在合理范围内。
2.2.5数据库视图设计
1.宿管管理视图-managerview
每个宿管可以查询到自己所管理楼栋的学生信息
2.首页-概览视图
显示系统的主要功能和统计数据,如宿舍总数、已入住学生数、空余床位数等。
3.维修申请视图
列出所有提交的维修申请,包括申请时间、宿舍号、维修内容、申请状态(待处理、处理中、已完成等)。提供处理维修申请的功能,如批准、拒绝、分配维修人员等。
2.2.6函数设计
1.用户登录与权限验证
用户登录分为管理员登录和学生登录。在两个对应表种分别查询输入的用户名和密码是否存在,如果存在,登陆成功,不存在则登陆失败,需要注册账号。
login(username, password)
: 验证用户登录信息,返回用户权限或错误信息。check_permission(user_id, function_name)
: 检查用户是否具有执行特定功能的权限。
2.宿舍信息管理
管理员可以查到相关信息,并对其进行增删改查等操作。
get_dormitory_list()
: 获取所有宿舍列表。get_dormitory_info(dormitory_id)
: 根据宿舍ID获取宿舍详细信息。add_dormitory(dormitory_info)
: 添加新宿舍信息。update_dormitory(dormitory_id, dormitory_info)
: 更新宿舍信息。delete_dormitory(dormitory_id)
: 删除宿舍信息。
3.维修管理
学生可以申请维修服务,管理员可以查询到维修信息,并为其分配维修人员。
report_repair(dormitory_id, description)
: 提交维修申请。get_repair_list(status=None)
: 获取维修申请列表,可根据状态筛选。update_repair_status(repair_id, status)
: 更新维修申请状态。assign_repair_worker(repair_id, worker_id)
: 为维修申请分配维修人员。
4.卫生检查管理
管理员检查宿舍为每个宿舍卫生打分,学生可以查询到分数。
inspect_hygiene(dormitory_id, score)
: 进行卫生检查并记录评分。get_hygiene_scores(dormitory_id, date_range=None)
: 获取宿舍卫生评分记录,可根据日期范围筛选。
5.通知与公告管理
管理员在主页展示板块可以发布一些重要的通知。
add_notice(title, content, date)
: 发布新通知或公告。get_notice_list(date_range=None)
: 获取通知或公告列表,可根据日期范围筛选。delete_notice(notice_id)
: 删除通知或公告。
2.2.7 存储过程设计
1.student
2.manager
3.dormitory_building
4.dormitory_room
5.information
6.hygiene
7.maintain
8.maintain_man
2.2.8 触发器设计
(1)如果有新公寓那么更新房间。
(2)如果删除公寓,那么必须在没有学生居住的情况下删除对应房间,将负责该公寓的舍管管理的楼栋置空。
(3)如果增加房间,那么同步的增加电费记录。
(4)如果删除房间,必须在没有学生住在对应房间的情况下,否则回滚,如果可以删除,那么同样删除电费记录。
3、系统实现
3.1数据库的建立
3.1.1表的创建
(1)学生基本信息表
CREATE TABLE student (
sno VARCHAR(20) NOT NULL,-- 学号
sname VARCHAR(50),-- 姓名
major VARCHAR(50),-- 专业
class VARCHAR(50),-- 班级
sex VARCHAR(10),-- 性别
building VARCHAR(50),-- 楼栋
room VARCHAR(10),-- 房间
contact_information VARCHAR(100),-- 联系方式
age INT,-- 年龄
code VARCHAR(20),-- 密码
bed VARCHAR(10),-- 床位
PRIMARY KEY (sno)-- 设置学号为主键
);
(2)公寓管理员信息表
CREATE TABLE manager (
msno VARCHAR(20) NOT NULL,-- 管理员编号
mname VARCHAR(50),-- 姓名
sex VARCHAR(10),-- 性别
age INT,-- 年龄
building VARCHAR(50),-- 管理楼栋
contact_information VARCHAR(100),-- 联系方式
code VARCHAR(20),-- 密码
PRIMARY KEY (msno)-- 设置管理员编号为主键
)
(3)公寓楼栋信息表
CREATE TABLE dormitory_building (
building VARCHAR(50) NOT NULL,-- 楼栋
manager_msno VARCHAR(20),-- 宿舍管理员编号
manager_mname VARCHAR(50),-- 姓名
classification VARCHAR(10),-- 分类 (男生/女生)
floor INT,-- 楼层数
PRIMARY KEY (building)-- 设置楼栋为主键
);
(4)公寓房间信息表
CREATE TABLE dormitory_room (
building VARCHAR(50) NOT NULL, -- 所在楼栋
room VARCHAR(10) NOT NULL, -- 房间号
size INT NOT NULL, -- 几人间
supervisor VARCHAR(50) NOT NULL, -- 舍长
contact_supervisor VARCHAR(15) NOT NULL,-- 舍长联系方式
is_full BOOLEAN NOT NULL, -- 是否住
);
(5)宿舍信息总表
CREATE TABLE information (
building VARCHAR(255) NOT NULL,
room INT NOT NULL,
date DATE NOT NULL,
maintain TEXT,
violation TEXT,
violation_bed VARCHAR(255),
energy_record DECIMAL(10, 2)
);(6)公寓卫生信息表
CREATE TABLE hygiene (
building VARCHAR(255) NOT NULL,
room INT NOT NULL,
bed VARCHAR(255) NOT NULL,
date DATE NOT NULL,
personal_score DECIMAL(3, 1),
pool_score DECIMAL(3, 1),
toilet_score DECIMAL(3, 1),
balcony_score DECIMAL(3, 1),
total_score DECIMAL(3, 1)
);(7)公寓维修信息表
CREATE TABLE maintain (
building VARCHAR(255) NOT NULL,
room INT NOT NULL,
date DATE NOT NULL,
reason TEXT NOT NULL,
maintain_man_sno VARCHAR(255) NOT NULL,
maintain_man_sname VARCHAR(255) NOT NULL,
maintain_man_contact VARCHAR(255) NOT NULL,
cost DECIMAL(10, 2) NOT NULL
);(8)维修工人信息表
CREATE TABLE maintain_man (
maintain_man_sno VARCHAR(255) PRIMARY KEY,
maintain_man_sname VARCHAR(255) NOT NULL,
free BOOLEAN DEFAULT TRUE,
maintain_man_contact VARCHAR(255) NOT NULL
);
3.1.2视图的建立
(1)公寓信息概览视图--可以查询到宿舍的基本信息,如当前住宿人数,当前宿舍数量,以及当前剩余空床数量。
CREATE VIEW dormitory_overview AS
SELECT
(SELECT COUNT(*) FROM dormitory_room) AS total_dormitories,
(SELECT COUNT(*) FROM student) AS total_students,
(SELECT SUM(spare) FROM dormitory_room) AS total_spare_beds;(2)维修记录视图--管理员可以查看申请的维修记录
CREATE VIEW manager_view_maintain
AS
SELECT date,building,room,maintain
from information
3.1.3索引的建立
(1)宿舍索引
CREATE INDEX idx_building_room ON dormitory_room (building, room);
(2)学生-宿舍索引
CREATE INDEX idx_sno_building_room ON student (sno, building, room);
3.2数据库访问设计
利用python中的库pymysql和关系型数据库mysql进行连接来访问数据库。
import pymysql
# 数据库连接信息
db_config = {
'host': 'Z',
'user': 'root',
'password': 'zyt2004520',
'database': 'dormitory'
}
# 连接数据库
def connect_db():
return pymysql.connect(**db_config)
3.3登录模块设计
在登录界面,首先需要选择登陆人员的身份,学生或是管理员。在这里,学生的信息只有能管理员进行增加或删除,学生自己不能在系统中注册信息。登陆的账号是管理员编号或者学号,密码则是他们各自设置的密码。如果以管理员身份登录,系统会从管理员信息表中查找匹配,若匹配不成功则输出错误提示,学生登陆类似。
登陆界面设计的代码实现:
# 居中显示窗口
def center_window(window, width=400, height=300):
screen_width = window.winfo_screenwidth()
screen_height = window.winfo_screenheight()
x = (screen_width // 2) - (width // 2)
y = (screen_height // 2) - (height // 2)
window.geometry(f'{width}x{height}+{x}+{y}')
# 设置窗口背景图片
def set_background(window, image_path="background.jpg"):
background_image = Image.open(image_path)
photo = ImageTk.PhotoImage(background_image)
# 加载公寓概览信息
def load_dormitory_info():
conn = connect_db()
cursor = conn.cursor()
cursor.execute("SELECT * FROM dormitory_overview")
result = cursor.fetchone()
cursor.close()
conn.close()
info_window = tk.Toplevel()
center_window(info_window, 800, 600)
info_window.title("公寓信息")
set_background(info_window) # 设置背景图片
info_label = tk.Label(info_window, text=f"宿舍总数: {result[0]}\n已入住学生数: {result[1]}\n空余床位数: {result[2]}",
font=("Arial", 16), bg='white')
info_label.pack(pady=20)
# 管理员登录界面
def admin_page(root):
clear_window(root)
root.title("管理员界面")
center_window(root, 800, 600)
set_background(root) # 设置背景图片
admin_label = tk.Label(root, text="欢迎管理员", bg='white')
admin_label.pack(pady=20)
# 添加公寓信息按钮
dorm_info_button = tk.Button(root, text="公寓信息", command=load_dormitory_info, bg='white')
dorm_info_button.pack(pady=10)
# 添加返回按钮
back_button = tk.Button(root, text="返回", command=lambda: login_page(root), bg='white')
back_button.pack(pady=10)
# 学生登录界面
def student_page(root):
clear_window(root)
root.title("学生界面")
center_window(root, 800, 600)
set_background(root) # 设置背景图片
student_label = tk.Label(root, text="欢迎学生", bg='white')
student_label.pack(pady=20)
# 添加返回按钮
back_button = tk.Button(root, text="返回", command=lambda: login_page(root), bg='white')
back_button.pack(pady=10)
# 验证登录信息
def validate_login(user_type, user_id, password):
conn = connect_db()
cursor = conn.cursor()
if user_type == 'admin':
query = "SELECT * FROM manager WHERE msno = %s AND code = %s"
elif user_type == 'student':
query = "SELECT * FROM student WHERE sno = %s AND code = %s"
cursor.execute(query, (user_id, password))
result = cursor.fetchone()
cursor.close()
conn.close()
return result
# 清除窗口中的所有组件
def clear_window(window):
for widget in window.winfo_children():
widget.destroy()
# 创建登录界面
def login_page(root=None):
if root is None:
root = tk.Tk()
else:
clear_window(root)
root.title("学生公寓系统")
root.geometry("800x600") # 设置窗口大小
root.resizable(False, False) # 固定窗口大小
center_window(root, 800, 600)
set_background(root) # 设置背景图片
# 设置字体样式
custom_font = tkfont.Font(family="Arial", size=20, weight="bold")
welcome_label = tk.Label(root, text="学生公寓", font=custom_font, bg="white")
welcome_label.pack(pady=20)
admin_button = tk.Button(root, text="管理员登录", command=lambda: admin_login(root), bg='white', width=15, height=2)
admin_button.place(relx=0.5, rely=0.4, anchor=tk.CENTER)
student_button = tk.Button(root, text="学生登录", command=lambda: student_login(root), bg='white', width=15, height=2)
student_button.place(relx=0.5, rely=0.5, anchor=tk.CENTER)
root.mainloop()
# 处理管理员登录按钮点击事件
def admin_login(root):
def login():
user_id = id_entry.get()
password = password_entry.get()
if validate_login('admin', user_id, password):
admin_page(root)
else:
messagebox.showerror("错误", "该用户不存在或密码错误")
login_window(root, 'admin', login)
# 处理学生登录按钮点击事件
def student_login(root):
def login():
user_id = id_entry.get()
password = password_entry.get()
if validate_login('student', user_id, password):
student_page(root)
else:
messagebox.showerror("错误", "该用户不存在或密码错误")
login_window(root, 'student', login)
# 创建登录窗口
def login_window(root, user_type, login_command):
global id_entry, password_entry
clear_window(root)
root.title(f"{'管理员' if user_type == 'admin' else '学生'}登录")
center_window(root, 800, 600)
set_background(root) # 设置背景图片
label_font = tkfont.Font(family="Arial", size=14, weight="bold")
entry_font = tkfont.Font(family="Arial", size=14)
id_label = tk.Label(root, text="账号", bg='green', font=label_font, fg='white') # 修改字体颜色为白色
id_label.place(relx=0.4, rely=0.35, anchor=tk.E)
id_entry = tk.Entry(root, font=entry_font)
id_entry.place(relx=0.4, rely=0.35, anchor=tk.W, width=200)
password_label = tk.Label(root, text="密码", bg='green', font=label_font, fg='white') # 修改字体颜色为白色
password_label.place(relx=0.4, rely=0.45, anchor=tk.E)
password_entry = tk.Entry(root, show="*", font=entry_font)
password_entry.place(relx=0.4, rely=0.45, anchor=tk.W, width=200)
login_button = tk.Button(root, text="登录", command=login_command, bg='green', font=label_font, width=10, fg='white') # 修改字体颜色为白色
login_button.place(relx=0.5, rely=0.55, anchor=tk.CENTER)
back_button = tk.Button(root, text="返回", command=lambda: login_page(root), bg='green', font=label_font, width=10, fg='white') # 修改字体颜色为白色
back_button.place(relx=0.5, rely=0.65, anchor=tk.CENTER)
# 启动登录界面
login_page()
实现效果图:
3.4各管理功能设计
3.4.1管理员审核维修信息
管理员可以看到维修申请记录,管理员需要审核维修记录,审核成功的维修申请需要为其分配空闲的维修工进行维修。
代码实现:
def refresh_records():
global selected_apply, selected_maintain_man
selected_apply = None
selected_maintain_man = None
output_text.delete(1.0, tk.END) # 清空输出文本框
# 清空两个列表框
displayApply.delete(0, tk.END)
displayMaittainMan.delete(0, tk.END)
# 查询维修申请信息
connection = pymysql.connect(host='localhost',
user='root',
password='caoyang@@2320',
database='dormitory')
cursor = connection.cursor()
sql_apply = "SELECT building, room, date, reason FROM maintain;"
cursor.execute(sql_apply)
for row in cursor.fetchall():
displayApply.insert(tk.END, ' '.join(map(str, row)))
# 查询空闲维修工信息
sql_maintain_man = "SELECT maintain_man_sno, maintain_man_sname FROM maintain_man WHERE free=1;"
cursor.execute(sql_maintain_man)
for row in cursor.fetchall():
displayMaittainMan.insert(tk.END, ' '.join(map(str, row)))
connection.close()
def on_select_apply(event):
global selected_apply
index_apply = displayApply.curselection()
if index_apply:
selected_apply = displayApply.get(index_apply)
output_text.insert(tk.END, f"选中的维修申请: {selected_apply}\n")
def on_select_maintain_man(event):
global selected_maintain_man
index_maintain_man = displayMaittainMan.curselection()
if index_maintain_man:
selected_maintain_man = displayMaittainMan.get(index_maintain_man)
output_text.insert(tk.END, f"选中的维修工: {selected_maintain_man}\n")
def on_button_assign_click():
global selected_apply, selected_maintain_man
if not selected_apply or not selected_maintain_man:
output_text.insert(tk.END, "请先选择要分派的维修申请和维修工。\n")
return
# 解析记录,获取关键字段
apply_fields = selected_apply.split()
maintain_man_fields = selected_maintain_man.split()
sql_update = f"""
UPDATE maintain
SET maintain_man_sname = '{maintain_man_fields[1]}',
maintain_man_contact = 'your_contact'
WHERE building = '{apply_fields[0]}' AND room = '{apply_fields[1]}' AND date = '{apply_fields[2]}';
"""
cursor.execute(sql_update)
connection.commit()
connection.close()
# 刷新记录
refresh_records()
# 创建应用程序对象
app = tk.Tk()
app.title('维修申请与分派')
app.geometry('800x600')
app.resizable(False, False) # 窗口大小不可调整
# 加载背景图片
background_image = Image.open("C:\\Users\曹杨\\Desktop\\sbackground.png")
background_photo = ImageTk.PhotoImage(background_image)
# 创建Canvas用于放置背景图片
canvas = tk.Canvas(app, width=800, height=600)
canvas.pack(fill="both", expand=True)
canvas.create_image(0, 0, image=background_photo, anchor="nw")
# 设置不同部分的字体
title_font = font.Font(size=14, weight='bold')
label_font = font.Font(size=10)
listbox_font = font.Font(size=10)
button_font = font.Font(size=10)
# 创建一个框架用于居中放置所有控件
frame = tk.Frame(canvas, bg="white")
frame.place(relx=0.5, rely=0.5, anchor="center", relwidth=0.8, relheight=0.8)
# 创建标签
label_apply = tk.Label(frame, text="维修申请", font=label_font, bg="white")
label_apply.place(relx=0, rely=0, relwidth=0.48, relheight=0.1)
label_maintain_man = tk.Label(frame, text="空闲维修工人", font=label_font, bg="white")
label_maintain_man.place(relx=0.52, rely=0, relwidth=0.48, relheight=0.1)
displayMaittainMan = tk.Listbox(frame, font=listbox_font)
displayMaittainMan.place(relx=0.52, rely=0.1, relwidth=0.48, relheight=0.6)
# 为 Listbox 绑定选择事件
displayApply.bind('<<ListboxSelect>>', on_select_apply)
displayMaittainMan.bind('<<ListboxSelect>>', on_select_maintain_man)
button_assign = tk.Button(frame, text='分派', command=on_button_assign_click, width=12, height=1, font=button_font)
button_assign.place(relx=0.52, rely=0.75, relwidth=0.48, relheight=0.1)
# 创建输出文本框
output_text = tk.Text(frame, height=4, width=80, font=listbox_font)
output_text.place(relx=0, rely=0.85, relwidth=1, relheight=0.15)
# 刷新记录
refresh_records()
# 运行应用程序主循环
app.mainloop()
实现效果图:
3.4.2 管理员查看宿舍总体情况
管理员登录后进入页面,有查看公寓信息按钮,点击进去,可以看到宿舍的一些基本情况,通过与数据库中概览视图的交互,可以把相关信息展现出来。
代码实现:
# 加载公寓概览信息
def load_dormitory_info():
conn = connect_db()
cursor = conn.cursor()
cursor.execute("SELECT * FROM dormitory_overview")
result = cursor.fetchone()
cursor.close()
conn.close()
info_window = tk.Toplevel()
center_window(info_window, 400, 300)
info_window.title("公寓信息")
info_label = tk.Label(info_window, text=f"宿舍总数: {result[0]}\n已入住学生数: {result[1]}\n空余床位数: {result[2]}",
font=("Arial", 16))
info_label.pack(pady=20)
# 管理员登录界面
def admin_page(root):
root.destroy() # 关闭登录窗口
admin_window = tk.Toplevel()
admin_window.geometry("300x200+100+100")
center_window(admin_window) # 窗口居中
admin_window.title("管理员界面")
admin_label = tk.Label(admin_window, text="欢迎管理员")
admin_label.pack(pady=20)
# 添加公寓信息按钮
dorm_info_button = tk.Button(admin_window, text="公寓信息", command=load_dormitory_info)
dorm_info_button.pack(pady=10)
# 添加返回按钮
back_button = tk.Button(admin_window, text="返回", command=lambda: login_page(admin_window))
back_button.pack(pady=10)
实现效果: