功能要求
一,数据录入
(录入、计算并保存,保存在student.txt文本文件)
二,数据的增、删、改
(注意及时保存,如果是增加或修改成绩,则要及时计算学分绩点)
三,奖学金,三好生的评定
四,排序
(1.按学分绩点2.按学号排序)
五,查询
(1.查询奖学金获得名单 2.查询三好生名单3.按学号查询某学生的信息 4.按姓名查询某学生的信息:5.查询全班的信息:6.返回)
六,退出
实现结果展示
注:
查询奖学金,三好学生;查询全班信息前之前,
可先选择按GPA排序或者按学号排序,这样查询结果也会按照GPA排序或者按学号排序,如图所示:
详细代码
import tkinter as tk
from tkinter import simpledialog, messagebox, Listbox
class Student:
def __init__(self, id, name, scores):
self.id = id
self.name = name
self.scores = scores # 这是一个字典
self.gpa = self.calculate_gpa()
def calculate_gpa(self):
credit_points = {
'高数一': 5.0,
'英语一': 4.5,
'体育一': 1.0,
'毛概': 2.5,
'高数二': 4.5,
'英语二': 4.0,
'Python': 4.0,
'体育二': 1.0
}
total_adjusted_score = 0
total_credits = 0
for subject, score in self.scores.items():
if subject in credit_points:
adjusted_score = (score / 10) - 5
credit = credit_points[subject]
total_adjusted_score += adjusted_score * credit
total_credits += credit
if total_credits == 0:
return 0
return total_adjusted_score / total_credits
def save_students(students):
with open('students.txt', 'w') as file:
for student in students:
scores_str = ','.join(map(str, student.scores.values()))
file.write(f'{student.id},{student.name},{scores_str}\n')
def load_students():
students = []
try:
with open('students.txt', 'r') as file:
lines = file.readlines()
for line in lines:
parts = line.strip().split(',')
id = parts[0]
name = parts[1]
scores = list(map(int, parts[2:]))
subjects = ['高数一', '英语一', '体育一', '毛概', '高数二', '英语二', 'Python', '体育二']
score_dict = dict(zip(subjects, scores))
students.append(Student(id, name, score_dict))
except FileNotFoundError:
pass
return students
def add_student():
id = simpledialog.askstring("输入", "学号:")
name = simpledialog.askstring("输入", "姓名:")
subjects = ['高数一', '英语一', '体育一', '毛概', '高数二', '英语二', 'Python', '体育二']
scores = {}
for subject in subjects:
score = int(simpledialog.askstring("输入", f"{subject}成绩:"))
scores[subject] = score
new_student = Student(id, name, scores)
students.append(new_student)
save_students(students)
messagebox.showinfo("成功", "学生添加成功")
refresh_student_list()
def modify_student():
selected_index = student_listbox.curselection()
if not selected_index:
messagebox.showwarning("警告", "请先选择一个学生")
return
selected_student = students[selected_index[0]]
new_name = simpledialog.askstring("输入", "新的姓名:", initialvalue=selected_student.name)
subjects = ['高数一', '英语一', '体育一', '毛概', '高数二', '英语二', 'Python', '体育二']
new_scores = {}
for subject in subjects:
score = int(simpledialog.askstring("输入", f"新的{subject}成绩:", initialvalue=selected_student.scores[subject]))
new_scores[subject] = score
selected_student.name = new_name
selected_student.scores = new_scores
selected_student.gpa = selected_student.calculate_gpa()
save_students(students)
messagebox.showinfo("成功", "学生信息修改成功")
refresh_student_list()
def delete_student():
selected_index = student_listbox.curselection()
if not selected_index:
messagebox.showwarning("警告", "请先选择一个学生")
return
students.pop(selected_index[0])
save_students(students)
messagebox.showinfo("成功", "学生删除成功")
refresh_student_list()
def refresh_student_list():
student_listbox.delete(0, tk.END)
for student in students:
student_listbox.insert(tk.END, f"{student.id} - {student.name} - GPA: {student.gpa:.2f}")
students = load_students()
def main_window():
global student_listbox
root = tk.Tk()
root.title("学生信息管理系统")
frame = tk.Frame(root)
frame.pack(padx=10, pady=10)
student_listbox = Listbox(frame, width=50, height=15)
student_listbox.pack(side=tk.LEFT, fill=tk.Y)
scrollbar = tk.Scrollbar(frame, orient=tk.VERTICAL, command=student_listbox.yview)
scrollbar.pack(side=tk.RIGHT, fill=tk.Y)
student_listbox.config(yscrollcommand=scrollbar.set)
button_frame = tk.Frame(root)
button_frame.pack(pady=10)
def sort_by_gpa():
global students
students.sort(key=lambda s: s.gpa, reverse=True)
refresh_student_list()
def sort_by_id():
global students
students.sort(key=lambda s: s.id)
refresh_student_list()
def query_student_by_id():
id = simpledialog.askstring("输入", "请输入学号:")
for student in students:
if student.id == id:
messagebox.showinfo("查询结果", f"学号: {student.id}\n姓名: {student.name}\nGPA: {student.gpa:.2f}")
return
messagebox.showwarning("警告", "未找到对应的学生")
def query_student_by_name():
name = simpledialog.askstring("输入", "请输入姓名:")
for student in students:
if student.name == name:
messagebox.showinfo("查询结果", f"学号: {student.id}\n姓名: {student.name}\nGPA: {student.gpa:.2f}")
return
messagebox.showwarning("警告", "未找到对应的学生")
def query_scholarship_awards():
scholarship_students = []
for student in students:
if 2.8 <= student.gpa < 3.2:
scholarship_students.append(f"{student.id} {student.name} GPA: {student.gpa:.2f} 三等奖")
elif 3.2 <= student.gpa < 3.6:
scholarship_students.append(f"{student.id} {student.name} GPA: {student.gpa:.2f} 二等奖")
elif student.gpa >= 3.6:
scholarship_students.append(f"{student.id} {student.name} GPA: {student.gpa:.2f} 一等奖")
result = "\n".join(scholarship_students)
if result:
messagebox.showinfo("奖学金获得者", result)
else:
messagebox.showinfo("奖学金获得者", "没有学生获得奖学金")
def query_excellent_students():
excellent_students = [s for s in students if s.gpa >= 4.0] # 假设GPA4.0 以上为三好学生
result = "\n".join([f"{s.id} {s.name} GPA: {s.gpa:.2f}" for s in excellent_students])
if result:
messagebox.showinfo("三好学生", result)
else:
messagebox.showinfo("三好学生", "没有学生获得三好学生称号")
def query_all_students():
result = "\n".join([f"{s.id} {s.name} GPA: {s.gpa:.2f}" for s in students])
messagebox.showinfo("全班学生信息", result if result else "没有学生信息")
def exit_program():
root.quit()
tk.Button(button_frame, text="添加学生", command=add_student).grid(row=0, column=0, padx=5)
tk.Button(button_frame, text="修改学生", command=modify_student).grid(row=0, column=1, padx=5)
tk.Button(button_frame, text="删除学生", command=delete_student).grid(row=0, column=2, padx=5)
tk.Button(button_frame, text="按GPA排序", command=sort_by_gpa).grid(row=1, column=0, padx=5)
tk.Button(button_frame, text="按学号排序", command=sort_by_id).grid(row=1, column=1, padx=5)
tk.Button(button_frame, text="查询奖学金学生", command=query_scholarship_awards).grid(row=2, column=0, padx=5)
tk.Button(button_frame, text="查询三好学生", command=query_excellent_students).grid(row=2, column=1, padx=5)
tk.Button(button_frame, text="按学号查询", command=query_student_by_id).grid(row=2, column=2, padx=5)
tk.Button(button_frame, text="按姓名查询", command=query_student_by_name).grid(row=2, column=3, padx=5)
tk.Button(button_frame, text="查询全班信息", command=query_all_students).grid(row=2, column=4, padx=5)
tk.Button(button_frame, text="退出", command=exit_program).grid(row=3, column=2, padx=5)
refresh_student_list()
root.mainloop()
students = load_students()
main_window()