目录
开发一个学生宿舍管理系统
概述
开发环境
程序设计
功能展示
主菜单
添加学生信息界面
删除学生信息界面
修改学生信息界面
查询学生信息界面
5. 完整代码
6. 总结
开发一个学生宿舍管理系统
在本文中,我们将介绍如何使用Python和Tkinter库开发一个简单的学生宿舍信息管理系统。该系统包括添加、删除、修改和查询学生信息的功能,并提供了图形用户界面以便用户操作。
概述
学生宿舍管理系统是一个用于管理学生宿舍信息的应用程序。它允许用户执行各种操作,如添加新的学生信息、删除现有的学生信息、修改学生信息以及查询特定学生信息。
开发环境
- Python编程语言
- Tkinter图形用户界面库
程序设计
首先,我们定义了几个类来表示系统中的不同数据:
Student
: 表示学生信息,包括学生ID、姓名、性别、年龄、院系和宿舍号。Dormitory
: 表示宿舍信息,包括宿舍号、容纳人数和当前人数。
然后,我们创建了一个名为StudentManagementSystem
的类来实现系统的各项功能,包括添加、删除、修改和查询学生信息。该类使用Tkinter库来构建图形用户界面,使用户可以通过简单的界面与系统交互。
功能展示
主菜单
添加学生信息界面
在这个界面中,用户可以输入学生的各项信息,并点击保存按钮来添加新的学生信息。
删除学生信息界面
用户可以输入要删除的学生ID,并点击删除按钮来删除该学生的信息。
修改学生信息界面
用户可以输入要修改的学生ID,并点击查询按钮来查询该学生的信息,然后在新窗口中修改学生的信息。
查询学生信息界面
用户可以输入要查询的学生ID,并点击查询按钮来查询该学生的信息,并显示在界面上。
5. 完整代码
import tkinter as tk
from tkinter import messagebox
class Student:
def __init__(self, id, name, gender, age, department, dormitory):
self.id = id
self.name = name
self.gender = gender
self.age = age
self.department = department
self.dormitory = dormitory
class Dormitory:
def __init__(self, dormitory, capacity, current):
self.dormitory = dormitory
self.capacity = capacity
self.current = current
class StudentManagementSystem:
def __init__(self, root):
self.root = root
self.root.title("学生宿舍管理系统")
self.create_widgets()
def create_widgets(self):
self.label_title = tk.Label(self.root, text="学生宿舍管理系统", font=("Helvetica", 20))
self.label_title.pack(pady=10)
self.add_button = tk.Button(self.root, text="添加学生信息", command=self.add_student)
self.add_button.pack()
self.delete_button = tk.Button(self.root, text="删除学生信息", command=self.delete_student)
self.delete_button.pack()
self.modify_button = tk.Button(self.root, text="修改学生信息", command=self.modify_student)
self.modify_button.pack()
self.query_button = tk.Button(self.root, text="查询学生信息", command=self.query_student)
self.query_button.pack()
def add_student(self):
self.add_window = tk.Toplevel(self.root)
self.add_window.title("添加学生信息")
self.id_label = tk.Label(self.add_window, text="学生ID:")
self.id_label.grid(row=0, column=0)
self.id_entry = tk.Entry(self.add_window)
self.id_entry.grid(row=0, column=1)
self.name_label = tk.Label(self.add_window, text="姓名:")
self.name_label.grid(row=1, column=0)
self.name_entry = tk.Entry(self.add_window)
self.name_entry.grid(row=1, column=1)
self.gender_label = tk.Label(self.add_window, text="性别:")
self.gender_label.grid(row=2, column=0)
self.gender_entry = tk.Entry(self.add_window)
self.gender_entry.grid(row=2, column=1)
self.age_label = tk.Label(self.add_window, text="年龄:")
self.age_label.grid(row=3, column=0)
self.age_entry = tk.Entry(self.add_window)
self.age_entry.grid(row=3, column=1)
self.department_label = tk.Label(self.add_window, text="院系:")
self.department_label.grid(row=4, column=0)
self.department_entry = tk.Entry(self.add_window)
self.department_entry.grid(row=4, column=1)
self.dormitory_label = tk.Label(self.add_window, text="宿舍号:")
self.dormitory_label.grid(row=5, column=0)
self.dormitory_entry = tk.Entry(self.add_window)
self.dormitory_entry.grid(row=5, column=1)
self.save_button = tk.Button(self.add_window, text="保存", command=self.save_student)
self.save_button.grid(row=6, column=0, columnspan=2)
def save_student(self):
id = self.id_entry.get()
name = self.name_entry.get()
gender = self.gender_entry.get()
age = self.age_entry.get()
department = self.department_entry.get()
dormitory = self.dormitory_entry.get()
if id and name and gender and age and department and dormitory:
with open("students.txt", "a") as file:
file.write(f"{id} {name} {gender} {age} {department} {dormitory}\n")
messagebox.showinfo("提示", "学生信息已添加。")
self.add_window.destroy()
else:
messagebox.showwarning("警告", "请输入完整的学生信息。")
def delete_student(self):
self.delete_window = tk.Toplevel(self.root)
self.delete_window.title("删除学生信息")
self.id_label = tk.Label(self.delete_window, text="要删除的学生ID:")
self.id_label.pack()
self.id_entry = tk.Entry(self.delete_window)
self.id_entry.pack()
self.delete_button = tk.Button(self.delete_window, text="删除", command=self.delete_student_entry)
self.delete_button.pack()
def delete_student_entry(self):
id = self.id_entry.get()
if id:
with open("students.txt", "r") as file:
lines = file.readlines()
with open("students.txt", "w") as file:
for line in lines:
if not line.startswith(f"{id} "):
file.write(line)
messagebox.showinfo("提示", "学生信息已删除。")
self.delete_window.destroy()
else:
messagebox.showwarning("警告", "请输入要删除的学生ID。")
def modify_student(self):
self.modify_window = tk.Toplevel(self.root)
self.modify_window.title("修改学生信息")
self.id_label = tk.Label(self.modify_window, text="要修改的学生ID:")
self.id_label.pack()
self.id_entry = tk.Entry(self.modify_window)
self.id_entry.pack()
self.search_button = tk.Button(self.modify_window, text="查询", command=self.search_student_for_update)
self.search_button.pack()
def search_student_for_update(self):
id = self.id_entry.get()
if id:
with open("students.txt", "r") as file:
for line in file:
student_info = line.split()
if student_info[0] == id:
self.show_update_window(student_info)
return
messagebox.showwarning("警告", "未找到该学生信息。")
else:
messagebox.showwarning("警告", "请输入要修改的学生ID。")
def show_update_window(self, student_info):
self.update_window = tk.Toplevel(self.root)
self.update_window.title("修改学生信息")
self.name_label = tk.Label(self.update_window, text="姓名:")
self.name_label.grid(row=0, column=0)
self.name_entry = tk.Entry(self.update_window)
self.name_entry.insert(0, student_info[1]) # Fill the current name
self.name_entry.grid(row=0, column=1)
self.gender_label = tk.Label(self.update_window, text="性别:")
self.gender_label.grid(row=1, column=0)
self.gender_entry = tk.Entry(self.update_window)
self.gender_entry.insert(0, student_info[2]) # Fill the current gender
self.gender_entry.grid(row=1, column=1)
self.age_label = tk.Label(self.update_window, text="年龄:")
self.age_label.grid(row=2, column=0)
self.age_entry = tk.Entry(self.update_window)
self.age_entry.insert(0, student_info[3]) # Fill the current age
self.age_entry.grid(row=2, column=1)
self.department_label = tk.Label(self.update_window, text="院系:")
self.department_label.grid(row=3, column=0)
self.department_entry = tk.Entry(self.update_window)
self.department_entry.insert(0, student_info[4]) # Fill the current department
self.department_entry.grid(row=3, column=1)
self.dormitory_label = tk.Label(self.update_window, text="宿舍号:")
self.dormitory_label.grid(row=4, column=0)
self.dormitory_entry = tk.Entry(self.update_window)
self.dormitory_entry.insert(0, student_info[5]) # Fill the current dormitory
self.dormitory_entry.grid(row=4, column=1)
self.save_button = tk.Button(self.update_window, text="保存", command=self.save_updated_student)
self.save_button.grid(row=5, column=0, columnspan=2)
def save_updated_student(self):
id = self.id_entry.get()
name = self.name_entry.get()
gender = self.gender_entry.get()
age = self.age_entry.get()
department = self.department_entry.get()
dormitory = self.dormitory_entry.get()
if id and name and gender and age and department and dormitory:
with open("students.txt", "r") as file:
lines = file.readlines()
with open("students.txt", "w") as file:
for line in lines:
if not line.startswith(f"{id} "):
file.write(line)
else:
file.write(f"{id} {name} {gender} {age} {department} {dormitory}\n")
messagebox.showinfo("提示", "学生信息已修改。")
self.update_window.destroy()
else:
messagebox.showwarning("警告", "请输入完整的学生信息。")
def search_student(self):
id = self.id_entry.get()
if id:
with open("students.txt", "r") as file:
for line in file:
student_info = line.split()
if student_info[0] == id:
self.show_query_result(student_info)
return
messagebox.showwarning("警告", "未找到该学生信息。")
else:
messagebox.showwarning("警告", "请输入要查询的学生ID。")
def show_query_result(self, student_info):
self.query_window = tk.Toplevel(self.root)
self.query_window.title("查询结果")
self.id_label = tk.Label(self.query_window, text=f"学生ID: {student_info[0]}")
self.id_label.pack()
self.name_label = tk.Label(self.query_window, text=f"姓名: {student_info[1]}")
self.name_label.pack()
self.gender_label = tk.Label(self.query_window, text=f"性别: {student_info[2]}")
self.gender_label.pack()
self.age_label = tk.Label(self.query_window, text=f"年龄: {student_info[3]}")
self.age_label.pack()
self.department_label = tk.Label(self.query_window, text=f"院系: {student_info[4]}")
self.department_label.pack()
self.dormitory_label = tk.Label(self.query_window, text=f"宿舍号: {student_info[5]}")
self.dormitory_label.pack()
def query_student(self):
self.query_window = tk.Toplevel(self.root)
self.query_window.title("查询学生信息")
self.id_label = tk.Label(self.query_window, text="要查询的学生ID:")
self.id_label.pack()
self.id_entry = tk.Entry(self.query_window)
self.id_entry.pack()
self.search_button = tk.Button(self.query_window, text="查询", command=self.search_student)
self.search_button.pack()
if __name__ == "__main__":
root = tk.Tk()
app = StudentManagementSystem(root)
root.mainloop()
6. 总结
通过本文的学习,我们了解了如何使用Python和Tkinter库开发一个简单的学生宿舍管理系统,并展示了系统的各项功能界面。这个系统可以作为学生信息管理的基础框架,可以根据实际需求进行扩展和定制。
希望本文对初学者理解Python和Tkinter的图形用户界面编程以及学生信息管理系统的开发有所帮助。如果您有任何问题或建议,请在评论中留言,我会尽力解答。
文章作者:命运之光