写一个Python使用Tkinter模块创建学生管理系统实验报告,实现基本的增加、删除、修改、查询、排序等功能。
时间: 2023-07-10 14:22:48 浏览: 101
好的,以下是一个简单的学生管理系统的实现,使用了Tkinter模块作为GUI界面,实现了基本的增加、删除、修改、查询、排序等功能。
```python
import tkinter as tk
from tkinter import messagebox
from tkinter import ttk
class Student:
def __init__(self, name, gender, age, grade):
self.name = name
self.gender = gender
self.age = age
self.grade = grade
class StudentManager:
def __init__(self):
self.students = []
def add_student(self, student):
self.students.append(student)
def remove_student(self, student):
self.students.remove(student)
def update_student(self, old_student, new_student):
index = self.students.index(old_student)
self.students[index] = new_student
def search_student(self, name):
for student in self.students:
if student.name == name:
return student
return None
def get_all_students(self):
return self.students
def sort_students_by_name(self):
self.students.sort(key=lambda student: student.name)
class StudentGUI:
def __init__(self):
self.student_manager = StudentManager()
self.window = tk.Tk()
self.window.title('学生管理系统')
self.create_widgets()
self.refresh_students_list()
def create_widgets(self):
# 创建标签和输入框
tk.Label(self.window, text='姓名').grid(row=0, column=0, padx=5, pady=5)
self.name_entry = tk.Entry(self.window)
self.name_entry.grid(row=0, column=1, padx=5, pady=5)
tk.Label(self.window, text='性别').grid(row=1, column=0, padx=5, pady=5)
self.gender_entry = tk.Entry(self.window)
self.gender_entry.grid(row=1, column=1, padx=5, pady=5)
tk.Label(self.window, text='年龄').grid(row=2, column=0, padx=5, pady=5)
self.age_entry = tk.Entry(self.window)
self.age_entry.grid(row=2, column=1, padx=5, pady=5)
tk.Label(self.window, text='成绩').grid(row=3, column=0, padx=5, pady=5)
self.grade_entry = tk.Entry(self.window)
self.grade_entry.grid(row=3, column=1, padx=5, pady=5)
# 创建按钮
tk.Button(self.window, text='添加', command=self.add_student).grid(row=4, column=0, padx=5, pady=5)
tk.Button(self.window, text='删除', command=self.remove_student).grid(row=4, column=1, padx=5, pady=5)
tk.Button(self.window, text='修改', command=self.update_student).grid(row=4, column=2, padx=5, pady=5)
tk.Button(self.window, text='查询', command=self.search_student).grid(row=4, column=3, padx=5, pady=5)
tk.Button(self.window, text='排序', command=self.sort_students).grid(row=4, column=4, padx=5, pady=5)
# 创建学生列表
self.students_listbox = tk.Listbox(self.window, height=10, width=50)
self.students_listbox.grid(row=5, column=0, columnspan=5, padx=5, pady=5)
def add_student(self):
name = self.name_entry.get()
gender = self.gender_entry.get()
age = self.age_entry.get()
grade = self.grade_entry.get()
if name and gender and age and grade:
student = Student(name, gender, age, grade)
self.student_manager.add_student(student)
self.refresh_students_list()
else:
messagebox.showinfo('错误', '请输入完整信息')
def remove_student(self):
selection = self.students_listbox.curselection()
if selection:
student = self.students_listbox.get(selection[0])
student = self.student_manager.search_student(student)
self.student_manager.remove_student(student)
self.refresh_students_list()
def update_student(self):
selection = self.students_listbox.curselection()
if selection:
old_student = self.students_listbox.get(selection[0])
old_student = self.student_manager.search_student(old_student)
name = self.name_entry.get()
gender = self.gender_entry.get()
age = self.age_entry.get()
grade = self.grade_entry.get()
if name and gender and age and grade:
new_student = Student(name, gender, age, grade)
self.student_manager.update_student(old_student, new_student)
self.refresh_students_list()
else:
messagebox.showinfo('错误', '请输入完整信息')
def search_student(self):
name = self.name_entry.get()
if name:
student = self.student_manager.search_student(name)
if student:
self.gender_entry.delete(0, tk.END)
self.gender_entry.insert(0, student.gender)
self.age_entry.delete(0, tk.END)
self.age_entry.insert(0, student.age)
self.grade_entry.delete(0, tk.END)
self.grade_entry.insert(0, student.grade)
else:
messagebox.showinfo('错误', '未找到该学生')
def sort_students(self):
self.student_manager.sort_students_by_name()
self.refresh_students_list()
def refresh_students_list(self):
self.students_listbox.delete(0, tk.END)
students = self.student_manager.get_all_students()
for student in students:
self.students_listbox.insert(tk.END, student.name)
if __name__ == '__main__':
app = StudentGUI()
app.window.mainloop()
```
该程序包含三个类:`Student`、`StudentManager`和`StudentGUI`。其中,`Student`类表示一个学生对象,包含姓名、性别、年龄、成绩四个属性;`StudentManager`类表示学生管理系统,包含添加、删除、修改、查询、排序等功能;`StudentGUI`类则是GUI界面的实现,包含标签、输入框、按钮、学生列表等控件,并将这些控件与学生管理系统的方法相连接。
您可以将以上代码保存为一个Python文件,运行该程序后,会弹出一个GUI界面,您可以在该界面上进行学生管理的各种操作。注意:该程序只是一个简单的学生管理系统的实现,如果您需要更加复杂的功能,可以根据需要修改代码。
阅读全文