使用python语言写一个代码拥有图形化界面的学生信息管理系统,实现基本的增加、删除、修改、查询、排序等功能
时间: 2023-07-10 18:20:03 浏览: 86
python写的学生信息管理系统
5星 · 资源好评率100%
好的,我可以为你提供一个基于Python的GUI学生信息管理系统的代码,使用Tkinter来构建图形化界面。这个系统可以实现添加、删除、修改、查询和排序学生信息的功能。以下是代码:
```
from tkinter import *
from tkinter import messagebox
import csv
class Student:
def __init__(self, name, age, gender, score):
self.name = name
self.age = age
self.gender = gender
self.score = score
class StudentManagementSystem:
def __init__(self, master):
self.master = master
master.title("学生信息管理系统")
master.geometry("400x300")
# 学生信息列表
self.students = []
# 标签和输入框
self.name_label = Label(master, text="姓名")
self.name_label.grid(row=0, column=0)
self.name_entry = Entry(master)
self.name_entry.grid(row=0, column=1)
self.age_label = Label(master, text="年龄")
self.age_label.grid(row=1, column=0)
self.age_entry = Entry(master)
self.age_entry.grid(row=1, column=1)
self.gender_label = Label(master, text="性别")
self.gender_label.grid(row=2, column=0)
self.gender_entry = Entry(master)
self.gender_entry.grid(row=2, column=1)
self.score_label = Label(master, text="成绩")
self.score_label.grid(row=3, column=0)
self.score_entry = Entry(master)
self.score_entry.grid(row=3, column=1)
# 按钮
self.add_button = Button(master, text="添加", command=self.add_student)
self.add_button.grid(row=4, column=0)
self.delete_button = Button(master, text="删除", command=self.delete_student)
self.delete_button.grid(row=4, column=1)
self.update_button = Button(master, text="修改", command=self.update_student)
self.update_button.grid(row=4, column=2)
self.search_button = Button(master, text="查询", command=self.search_student)
self.search_button.grid(row=5, column=0)
self.sort_button = Button(master, text="排序", command=self.sort_student)
self.sort_button.grid(row=5, column=1)
# 显示学生列表
self.student_listbox = Listbox(master)
self.student_listbox.grid(row=6, column=0, columnspan=3)
# 读取已有文件中的学生信息
try:
with open("students.csv", "r", newline="") as file:
reader = csv.reader(file)
for row in reader:
student = Student(row[0], row[1], row[2], row[3])
self.students.append(student)
self.student_listbox.insert(END, student.name)
except FileNotFoundError:
pass
# 添加学生
def add_student(self):
name = self.name_entry.get()
age = self.age_entry.get()
gender = self.gender_entry.get()
score = self.score_entry.get()
if not name or not age or not gender or not score:
messagebox.showerror("错误", "请填写完整信息")
return
student = Student(name, age, gender, score)
self.students.append(student)
self.student_listbox.insert(END, student.name)
self.save_to_file()
self.clear_entries()
# 删除学生
def delete_student(self):
selection = self.student_listbox.curselection()
if not selection:
return
index = selection[0]
student = self.students.pop(index)
self.student_listbox.delete(index)
self.save_to_file()
self.clear_entries()
# 修改学生
def update_student(self):
selection = self.student_listbox.curselection()
if not selection:
return
index = selection[0]
student = self.students[index]
name = self.name_entry.get()
age = self.age_entry.get()
gender = self.gender_entry.get()
score = self.score_entry.get()
if name:
student.name = name
if age:
student.age = age
if gender:
student.gender = gender
if score:
student.score = score
self.student_listbox.delete(index)
self.student_listbox.insert(index, student.name)
self.save_to_file()
self.clear_entries()
# 查询学生
def search_student(self):
name = self.name_entry.get()
age = self.age_entry.get()
gender = self.gender_entry.get()
score = self.score_entry.get()
if not name and not age and not gender and not score:
messagebox.showerror("错误", "请填写要查询的信息")
return
self.student_listbox.delete(0, END)
for student in self.students:
if (name and name.lower() in student.name.lower()) or (age and age == student.age) or \
(gender and gender.lower() == student.gender.lower()) or (score and score == student.score):
self.student_listbox.insert(END, student.name)
# 排序学生
def sort_student(self):
self.students.sort(key=lambda student: student.score, reverse=True)
self.student_listbox.delete(0, END)
for student in self.students:
self.student_listbox.insert(END, student.name)
self.save_to_file()
# 保存学生信息到文件
def save_to_file(self):
with open("students.csv", "w", newline="") as file:
writer = csv.writer(file)
for student in self.students:
writer.writerow([student.name, student.age, student.gender, student.score])
# 清空输入框
def clear_entries(self):
self.name_entry.delete(0, END)
self.age_entry.delete(0, END)
self.gender_entry.delete(0, END)
self.score_entry.delete(0, END)
root = Tk()
app = StudentManagementSystem(root)
root.mainloop()
```
这个代码实现了基本的增加、删除、修改、查询和排序学生信息的功能。可以通过填写学生的姓名、年龄、性别和成绩来添加学生,也可以通过选择学生并修改或删除来修改或删除学生。同时,提供了查询和排序的功能。所有学生信息都存储在一个CSV文件中,以便持久化存储学生信息。
阅读全文