基于python的学生成绩管理系统(GUI)

利用python语言实现成绩管理系统的实现,以某班学生为例,实现以下功能:

(1)    添加学生信息以及其九科成绩信息;

(2)    将学生信息保存在文件中;

(3)    修改和删除学生信息;

(4)    查询学生信息;

(5)    显示已经添加的所有学生信息。

设计要求:

1.具有主菜单界面显示。

2.有良好的GUI人机交互界面。

3.提示错误格式信息并重新输入。

4.具有输入验证和错误提示。 

import tkinter as tk
from tkinter import messagebox, simpledialog, filedialog


class Student:
    def __init__(self, roll_no, name, scores):
        self.roll_no = roll_no
        self.name = name
        self.scores = scores

    def __str__(self):
        return f"Roll No.: {self.roll_no}, Name: {self.name}, Scores: {self.scores}"


class ScoreManager:
    def __init__(self, root):
        self.root = root
        self.root.title("成绩管理系统")
        self.root.geometry("500x700")

        self.students = []
        self.selected_student_index = None

        # 创建学生信息输入的widgets(略)
        self.roll_no_var = tk.StringVar()
        self.name_var = tk.StringVar()
        self.scores_vars = []
        for _ in range(9):
            var = tk.StringVar()
            self.scores_vars.append(var)

        # 添加一个列表框来显示学生信息
        self.student_listbox = tk.Listbox(root, width=50, height=10)
        self.student_listbox.grid(row=18, column=0, columnspan=2)
        self.student_listbox.bind('<<ListboxSelect>>', self.on_student_selected)

        # 创建GUI组件
        tk.Label(root, text="学号").grid(row=0, column=0)
        tk.Entry(root, textvariable=self.roll_no_var).grid(row=0, column=1)

        tk.Label(root, text="姓名").grid(row=1, column=0)
        tk.Entry(root, textvariable=self.name_var).grid(row=1, column=1)

        for i in range(9):
            tk.Label(root, text=f"科目{i + 1}").grid(row=i + 2, column=0)
            tk.Entry(root, textvariable=self.scores_vars[i]).grid(row=i + 2, column=1)

        # 添加学生信息的按钮
        add_button = tk.Button(root, text="添加学生", command=self.add_student)
        add_button.grid(row=11, column=0, columnspan=2)

        # 显示所有学生信息的按钮
        show_button = tk.Button(root, text="显示所有学生", command=self.show_all_students)
        show_button.grid(row=12, column=0, columnspan=2)

        # 修改学生信息的按钮
        modify_button = tk.Button(root, text="修改学生", command=self.modify_student)
        modify_button.grid(row=13, column=0, columnspan=2)

        # 删除学生信息的按钮
        delete_button = tk.Button(root, text="删除学生", command=self.delete_student)
        delete_button.grid(row=14, column=0, columnspan=2)

        # 查询学生信息的按钮
        search_button = tk.Button(root, text="查询学生", command=self.search_student)
        search_button.grid(row=15, column=0, columnspan=2)

        # 保存学生信息的按钮
        save_button = tk.Button(root, text="保存信息到文件", command=self.save_to_file)
        save_button.grid(row=16, column=0, columnspan=2)

        # 从文件加载学生信息的按钮
        load_button = tk.Button(root, text="从文件加载信息", command=self.load_from_file)
        load_button.grid(row=17, column=0, columnspan=2)

    def on_student_selected(self, event):
        self.selected_student_index = self.student_listbox.curselection()[0]

    def add_student(self):
        try:
            roll_no = int(self.roll_no_var.get())
            name = self.name_var.get()
            scores = [int(var.get()) for var in self.scores_vars]  # 使用 self.scores_vars 而不是 self.subjects_vars
            student = Student(roll_no, name, scores)
            self.students.append(student)
            self.student_listbox.insert(tk.END, str(student))  # 可以选择在这里直接更新列表框
            messagebox.showinfo("成功", "学生信息已添加")
        except ValueError as e:
            messagebox.showerror("错误", "请输入正确的信息格式")

    def show_all_students(self):
        self.student_listbox.delete(0, tk.END)
        for index, student in enumerate(self.students):
            self.student_listbox.insert(tk.END, str(student))

    def modify_student(self):
        if self.selected_student_index is None:
            messagebox.showinfo("提示", "请先选择一个学生")
            return

        student = self.students[self.selected_student_index]
        new_name = simpledialog.askstring("修改", "请输入新姓名:", parent=self.root, initialvalue=student.name)
        if not new_name:
            return

        new_scores = []
        for i in range(9):
            score = simpledialog.askfloat(f"修改科目{i + 1}成绩", f"请输入新成绩:", parent=self.root,
                                          initialvalue=student.scores[i])
            if score is not None:
                new_scores.append(score)
            else:
                break  # 如果用户取消,则不继续修改其他科目

        if len(new_scores) == 9:
            student.name = new_name
            student.scores = new_scores
            self.show_all_students()
            self.save_to_file()
            messagebox.showinfo("成功", "学生信息已修改")

    def delete_student(self):
        if self.selected_student_index is None:
            messagebox.showinfo("提示", "请先选择一个学生")
            return

        if messagebox.askokcancel("确认", "确定要删除这个学生吗?"):
            del self.students[self.selected_student_index]
            self.selected_student_index = None
            self.student_listbox.delete(self.student_listbox.curselection())
            self.save_to_file()

    def search_student(self):
        roll_no = simpledialog.askinteger("查询", "请输入学号来查询学生:", parent=self.root)
        if roll_no is None:
            return

        for index, student in enumerate(self.students):
            if student.roll_no == roll_no:
                self.student_listbox.selection_clear(0, tk.END)
                self.student_listbox.selection_set(index)
                self.student_listbox.see(index)
                self.on_student_selected(None)  # 模拟选择事件
                break
        else:
            messagebox.showinfo("未找到", "未找到该学号的学生")

    def save_to_file(self):
        with open('students.txt', 'w') as file:
            for student in self.students:
                file.write(f"{student.roll_no}, {student.name}, {', '.join(map(str, student.scores))}\n")

    def load_from_file(self):
        try:
            with open('students.txt', 'r') as file:
                self.students = []
                for line in file:
                    roll_no, name, scores_str = line.strip().split(', ', 2)
                    scores = list(map(int, scores_str.split(', ')))
                    student = Student(int(roll_no), name, scores)
                    self.students.append(student)
            self.show_all_students()
            messagebox.showinfo("成功", "信息已从文件加载")
        except FileNotFoundError:
            messagebox.showerror("错误", "文件不存在")
        except Exception as e:
            messagebox.showerror("错误", f"加载文件时出错: {e}")


if __name__ == "__main__":
    root = tk.Tk()
    score_manager = ScoreManager(root)
    root.mainloop()

 还有一版主页查看

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值
>