现有学生成绩信息,内容如下 姓名 学号 语文 数学 英语 张明明 01 67 78 82 李成友 02 78 91 88 张辉灿 03 68 82 56 王露 04564577 陈东明 05 67 38 47 .... ... ... ... ... 请用 Python 编写一个有 GUI 界面的学生信息管理系统,实现,以下功能: (1)学生信息数据要以文件的形式保存,能增加、删除和修改学生信息 (2)能实现按姓名查询和按学号查询两种方式查询学生信息 (3)输入任意一个课程名(如数学)和一个分数段(如 60–70),统计出在此分数段的学生情况. (4)能对用户指定的任意课程名,按成绩升序或降序排列学生数据并显示排序结果(使用表 格的形式显示排序后的输出结果)(使用多种方法排序者,加分)
时间: 2023-06-16 13:04:12 浏览: 541
以下是一个基于Python Tkinter的简单的学生信息管理系统GUI程序的示例代码,可供参考:
```python
import tkinter as tk
import tkinter.messagebox as messagebox
class Student:
def __init__(self, name, id, chinese, math, english):
self.name = name
self.id = id
self.chinese = chinese
self.math = math
self.english = english
class StudentManagementSystem:
def __init__(self):
self.students = []
self.load_students()
self.init_ui()
def load_students(self):
try:
with open('students.txt', 'r') as f:
lines = f.readlines()
for line in lines:
parts = line.strip().split()
name = parts[0]
id = parts[1]
chinese = int(parts[2])
math = int(parts[3])
english = int(parts[4])
student = Student(name, id, chinese, math, english)
self.students.append(student)
except:
pass
def save_students(self):
with open('students.txt', 'w') as f:
for student in self.students:
f.write('{} {} {} {} {}\n'.format(student.name, student.id, student.chinese, student.math, student.english))
def add_student(self):
name = self.name_entry.get().strip()
id = self.id_entry.get().strip()
chinese = int(self.chinese_entry.get().strip())
math = int(self.math_entry.get().strip())
english = int(self.english_entry.get().strip())
student = Student(name, id, chinese, math, english)
self.students.append(student)
self.save_students()
self.update_student_listbox()
def delete_student(self):
selection = self.student_listbox.curselection()
if len(selection) == 0:
messagebox.showerror('Error', 'Please select a student to delete.')
return
index = selection[0]
del self.students[index]
self.save_students()
self.update_student_listbox()
def update_student(self):
selection = self.student_listbox.curselection()
if len(selection) == 0:
messagebox.showerror('Error', 'Please select a student to update.')
return
index = selection[0]
name = self.name_entry.get().strip()
id = self.id_entry.get().strip()
chinese = int(self.chinese_entry.get().strip())
math = int(self.math_entry.get().strip())
english = int(self.english_entry.get().strip())
student = Student(name, id, chinese, math, english)
self.students[index] = student
self.save_students()
self.update_student_listbox()
def search_student_by_name(self):
name = self.name_query_entry.get().strip()
students = [s for s in self.students if s.name == name]
self.display_students(students)
def search_student_by_id(self):
id = self.id_query_entry.get().strip()
students = [s for s in self.students if s.id == id]
self.display_students(students)
def search_student_by_score_range(self):
course = self.course_query_entry.get().strip()
score_range = self.score_query_entry.get().strip()
start_score, end_score = score_range.split('-')
start_score = int(start_score)
end_score = int(end_score)
if course == 'Chinese':
students = [s for s in self.students if s.chinese >= start_score and s.chinese <= end_score]
elif course == 'Math':
students = [s for s in self.students if s.math >= start_score and s.math <= end_score]
elif course == 'English':
students = [s for s in self.students if s.english >= start_score and s.english <= end_score]
else:
students = []
self.display_students(students)
def sort_students(self):
course = self.course_sort_entry.get().strip()
if course == 'Chinese':
students = sorted(self.students, key=lambda x: x.chinese)
elif course == 'Math':
students = sorted(self.students, key=lambda x: x.math)
elif course == 'English':
students = sorted(self.students, key=lambda x: x.english)
else:
students = self.students
if self.sort_descending.get():
students = reversed(students)
self.display_students(students)
def update_student_entry(self, event):
selection = self.student_listbox.curselection()
if len(selection) == 0:
return
index = selection[0]
student = self.students[index]
self.name_entry.delete(0, tk.END)
self.name_entry.insert(0, student.name)
self.id_entry.delete(0, tk.END)
self.id_entry.insert(0, student.id)
self.chinese_entry.delete(0, tk.END)
self.chinese_entry.insert(0, student.chinese)
self.math_entry.delete(0, tk.END)
self.math_entry.insert(0, student.math)
self.english_entry.delete(0, tk.END)
self.english_entry.insert(0, student.english)
def update_student_listbox(self):
self.student_listbox.delete(0, tk.END)
for student in self.students:
self.student_listbox.insert(tk.END, '{} ({})'.format(student.name, student.id))
def display_students(self, students):
self.result_listbox.delete(0, tk.END)
for student in students:
self.result_listbox.insert(tk.END, '{} ({}) Chinese:{} Math:{} English:{}'.format(student.name, student.id, student.chinese, student.math, student.english))
def init_ui(self):
self.root = tk.Tk()
self.root.title('Student Management System')
# Add student frame
add_student_frame = tk.LabelFrame(self.root, text='Add Student')
add_student_frame.grid(row=0, column=0, padx=10, pady=10)
tk.Label(add_student_frame, text='Name:').grid(row=0, column=0, padx=5, pady=5)
self.name_entry = tk.Entry(add_student_frame)
self.name_entry.grid(row=0, column=1, padx=5, pady=5)
tk.Label(add_student_frame, text='ID:').grid(row=1, column=0, padx=5, pady=5)
self.id_entry = tk.Entry(add_student_frame)
self.id_entry.grid(row=1, column=1, padx=5, pady=5)
tk.Label(add_student_frame, text='Chinese:').grid(row=2, column=0, padx=5, pady=5)
self.chinese_entry = tk.Entry(add_student_frame)
self.chinese_entry.grid(row=2, column=1, padx=5, pady=5)
tk.Label(add_student_frame, text='Math:').grid(row=3, column=0, padx=5, pady=5)
self.math_entry = tk.Entry(add_student_frame)
self.math_entry.grid(row=3, column=1, padx=5, pady=5)
tk.Label(add_student_frame, text='English:').grid(row=4, column=0, padx=5, pady=5)
self.english_entry = tk.Entry(add_student_frame)
self.english_entry.grid(row=4, column=1, padx=5, pady=5)
add_button = tk.Button(add_student_frame, text='Add', command=self.add_student)
add_button.grid(row=5, column=0, columnspan=2, padx=5, pady=5)
# Delete/update student frame
delete_update_student_frame = tk.LabelFrame(self.root, text='Delete/Update Student')
delete_update_student_frame.grid(row=1, column=0, padx=10, pady=10)
self.student_listbox = tk.Listbox(delete_update_student_frame)
self.student_listbox.grid(row=0, column=0, rowspan=4, padx=5, pady=5)
self.student_listbox.bind('<<ListboxSelect>>', self.update_student_entry)
delete_button = tk.Button(delete_update_student_frame, text='Delete', command=self.delete_student)
delete_button.grid(row=0, column=1, padx=5, pady=5)
update_button = tk.Button(delete_update_student_frame, text='Update', command=self.update_student)
update_button.grid(row=1, column=1, padx=5, pady=5)
tk.Label(delete_update_student_frame, text='Name:').grid(row=0, column=2, padx=5, pady=5)
self.name_entry = tk.Entry(delete_update_student_frame)
self.name_entry.grid(row=0, column=3, padx=5, pady=5)
tk.Label(delete_update_student_frame, text='ID:').grid(row=1, column=2, padx=5, pady=5)
self.id_entry = tk.Entry(delete_update_student_frame)
self.id_entry.grid(row=1, column=3, padx=5, pady=5)
tk.Label(delete_update_student_frame, text='Chinese:').grid(row=2, column=2, padx=5, pady=5)
self.chinese_entry = tk.Entry(delete_update_student_frame)
self.chinese_entry.grid(row=2, column=3, padx=5, pady=5)
tk.Label(delete_update_student_frame, text='Math:').grid(row=3, column=2, padx=5, pady=5)
self.math_entry = tk.Entry(delete_update_student_frame)
self.math_entry.grid(row=3, column=3, padx=5, pady=5)
tk.Label(delete_update_student_frame, text='English:').grid(row=4, column=2, padx=5, pady=5)
self.english_entry = tk.Entry(delete_update_student_frame)
self.english_entry.grid(row=4, column=3, padx=5, pady=5)
# Search student frame
search_student_frame = tk.LabelFrame(self.root, text='Search Student')
search_student_frame.grid(row=0, column=1, padx=10, pady=10)
tk.Label(search_student_frame, text='Search by Name:').grid(row=0, column=0, padx=5, pady=5)
self.name_query_entry = tk.Entry(search_student_frame)
self.name_query_entry.grid(row=0, column=1, padx=5, pady=5)
name_query_button = tk.Button(search_student_frame, text='Search', command=self.search_student_by_name)
name_query_button.grid(row=0, column=2, padx=5, pady=5)
tk.Label(search_student_frame, text='Search by ID:').grid(row=1, column=0, padx=5, pady=5)
self.id_query_entry = tk.Entry(search_student_frame)
self.id_query_entry.grid(row=1, column=1, padx=5, pady=5)
id_query_button = tk.Button(search_student_frame, text='Search', command=self.search_student_by_id)
id_query_button.grid(row=1, column=2, padx=5, pady=5)
tk.Label(search_student_frame, text='Search by Score Range:').grid(row=2, column=0, padx=5, pady=5)
self.course_query_entry = tk.Entry(search_student_frame)
self.course_query_entry.grid(row=2, column=1, padx=5, pady=5)
self.score_query_entry = tk.Entry(search_student_frame)
self.score_query_entry.grid(row=2, column=2, padx=5, pady=5)
score_query_button = tk.Button(search_student_frame, text='Search', command=self.search_student_by_score_range)
score_query_button.grid(row=2, column=3, padx=5, pady=5)
self.result_listbox = tk.Listbox(search_student_frame)
self.result_listbox.grid(row=3, column=0, columnspan=4, padx=5, pady=5)
# Sort student frame
sort_student_frame = tk.LabelFrame(self.root, text='Sort Student')
sort_student_frame.grid(row=1, column=1, padx=10, pady=10)
tk.Label(sort_student_frame, text='Course:').grid(row=0, column=0, padx=5, pady=5)
self.course_sort_entry = tk.Entry(sort_student_frame)
self.course_sort_entry.grid(row=0, column=1, padx=5, pady=5)
tk.Label(sort_student_frame, text='Descending:').grid(row=1, column=0, padx=5, pady=5)
self.sort_descending = tk.BooleanVar()
descending_checkbox = tk.Checkbutton(sort_student_frame, variable=self.sort_descending)
descending_checkbox.grid(row=1, column=1, padx=5, pady=5)
sort_button = tk.Button(sort_student_frame, text='Sort', command=self.sort_students)
sort_button.grid(row=2, column=0, columnspan=2, padx=5, pady=5)
self.sorted_listbox = tk.Listbox(sort_student_frame)
self.sorted_listbox.grid(row=3, column=0, columnspan=2, padx=5, pady=5)
self.update_student_listbox()
self.root.mainloop()
if __name__ == '__main__':
app = StudentManagementSystem()
```
该程序使用了Python的Tkinter模块来构建GUI界面,实现了学生信息的增、删、改、查、排序等功能,并将学生信息以文件的形式保存。用户可以通过输入学生姓名、学号、课程名和分数段等条件来查询、统计或排序学生信息。由于篇幅有限,该程序还有很多可以改进的地方,比如增加输入验证、使用数据库等,但这个示例程序可以作为学生信息管理系统的参考实现。
阅读全文