用python写一个学生管理系统文件版,功能包括,新增学生信息,删除学生信息,修改学生信息,查看所有学生信息,查看单个学生信息用grid布局
时间: 2024-02-28 12:53:43 浏览: 156
好的,我可以为您提供一个基本的学生管理系统的代码,其中包括了您所要求的新增、删除、修改、查看所有和查看单个学生信息的功能。此外,我还将使用Python中的Tkinter库实现GUI界面,并采用grid布局来排列窗口中的控件。请注意,为了实现文件版,本程序将使用CSV文件来储存学生信息。
以下是完整的代码:
```python
import tkinter as tk
import csv
class Student:
def __init__(self, name, age, gender, grade):
self.name = name
self.age = age
self.gender = gender
self.grade = grade
class StudentManagementSystem:
def __init__(self, master):
self.master = master
self.master.title("学生管理系统")
self.master.geometry("600x400")
# 创建控件
self.label_name = tk.Label(master, text="姓名")
self.label_age = tk.Label(master, text="年龄")
self.label_gender = tk.Label(master, text="性别")
self.label_grade = tk.Label(master, text="成绩")
self.entry_name = tk.Entry(master)
self.entry_age = tk.Entry(master)
self.entry_gender = tk.Entry(master)
self.entry_grade = tk.Entry(master)
self.button_add = tk.Button(master, text="新增学生", command=self.add_student)
self.button_delete = tk.Button(master, text="删除学生", command=self.delete_student)
self.button_update = tk.Button(master, text="修改学生信息", command=self.update_student)
self.button_show_all = tk.Button(master, text="查看所有学生", command=self.show_all_students)
self.button_show_single = tk.Button(master, text="查看单个学生", command=self.show_single_student)
self.text_area = tk.Text(master, width=50, height=10)
# 排列控件
self.label_name.grid(row=0, column=0)
self.label_age.grid(row=1, column=0)
self.label_gender.grid(row=2, column=0)
self.label_grade.grid(row=3, column=0)
self.entry_name.grid(row=0, column=1)
self.entry_age.grid(row=1, column=1)
self.entry_gender.grid(row=2, column=1)
self.entry_grade.grid(row=3, column=1)
self.button_add.grid(row=4, column=0)
self.button_delete.grid(row=4, column=1)
self.button_update.grid(row=4, column=2)
self.button_show_all.grid(row=5, column=0)
self.button_show_single.grid(row=5, column=1)
self.text_area.grid(row=6, columnspan=3)
def add_student(self):
# 从界面获取学生信息
name = self.entry_name.get()
age = self.entry_age.get()
gender = self.entry_gender.get()
grade = self.entry_grade.get()
# 创建学生实例
student = Student(name, age, gender, grade)
# 将学生信息写入CSV文件
with open("students.csv", mode="a", newline="") as file:
writer = csv.writer(file)
writer.writerow([student.name, student.age, student.gender, student.grade])
# 清空输入框
self.entry_name.delete(0, tk.END)
self.entry_age.delete(0, tk.END)
self.entry_gender.delete(0, tk.END)
self.entry_grade.delete(0, tk.END)
def delete_student(self):
# 从界面获取学生姓名
name = self.entry_name.get()
# 从CSV文件中删除该学生信息
with open("students.csv", mode="r") as file:
reader = csv.reader(file)
rows = [row for row in reader if row[0] != name]
with open("students.csv", mode="w", newline="") as file:
writer = csv.writer(file)
writer.writerows(rows)
# 清空输入框
self.entry_name.delete(0, tk.END)
def update_student(self):
# 从界面获取学生信息
name = self.entry_name.get()
age = self.entry_age.get()
gender = self.entry_gender.get()
grade = self.entry_grade.get()
# 从CSV文件中更新该学生信息
with open("students.csv", mode="r") as file:
reader = csv.reader(file)
rows = []
for row in reader:
if row[0] == name:
row[1] = age
row[2] = gender
row[3] = grade
rows.append(row)
with open("students.csv", mode="w", newline="") as file:
writer = csv.writer(file)
writer.writerows(rows)
# 清空输入框
self.entry_name.delete(0, tk.END)
self.entry_age.delete(0, tk.END)
self.entry_gender.delete(0, tk.END)
self.entry_grade.delete(0, tk.END)
def show_all_students(self):
# 从CSV文件中读取所有学生信息
with open("students.csv", mode="r") as file:
reader = csv.reader(file)
students = [Student(row[0], row[1], row[2], row[3]) for row in reader]
# 在界面中显示所有学生信息
self.text_area.delete("1.0", tk.END)
for student in students:
self.text_area.insert(tk.END, f"姓名:{student.name}\n年龄:{student.age}\n性别:{student.gender}\n成绩:{student.grade}\n\n")
def show_single_student(self):
# 从界面获取学生姓名
name = self.entry_name.get()
# 从CSV文件中读取该学生信息
with open("students.csv", mode="r") as file:
reader = csv.reader(file)
for row in reader:
if row[0] == name:
student = Student(row[0], row[1], row[2], row[3])
break
# 在界面中显示该学生信息
self.text_area.delete("1.0", tk.END)
self.text_area.insert(tk.END, f"姓名:{student.name}\n年龄:{student.age}\n性别:{student.gender}\n成绩:{student.grade}\n\n")
# 创建主窗口
root = tk.Tk()
# 运行程序
app = StudentManagementSystem(root)
root.mainloop()
```
这个程序实现了您所要求的所有功能,并且使用了文件储存学生信息。如果您需要修改代码以适应自己的需求,可以根据注释进行修改。希望能够对您有所帮助!
阅读全文