用python写一个学生管理系统文件版(保存到文本文档中)功能包括新增学生信息,修改学生信息,删除学生信息,查看单个学生信息,查看所有学生信息,用grid布局
时间: 2024-03-02 22:51:27 浏览: 50
好的,我可以帮您解答这个问题。
首先,您需要定义一个学生类,包含学生的姓名、年龄、性别等信息。然后,您需要定义一个学生管理系统类,包含添加学生、修改学生、删除学生、查看单个学生、查看所有学生等功能。
接下来,您需要将学生信息保存到文本文档中。可以使用 Python 内置的文件操作函数来实现。
最后,为了实现 UI 界面,您可以使用 Python 的 Tkinter 模块,并使用 Grid 布局来排列界面元素。
以下是一个可能的实现,仅供参考:
```python
import tkinter as tk
class Student:
def __init__(self, name, age, gender):
self.name = name
self.age = age
self.gender = gender
class StudentManager:
def __init__(self):
self.students = []
def add_student(self, student):
self.students.append(student)
def update_student(self, index, student):
self.students[index] = student
def delete_student(self, index):
del self.students[index]
def get_student(self, index):
return self.students[index]
def get_all_students(self):
return self.students
def save_to_file(self, filename):
with open(filename, 'w') as f:
for student in self.students:
f.write(f'{student.name},{student.age},{student.gender}\n')
def load_from_file(self, filename):
self.students = []
with open(filename, 'r') as f:
for line in f:
name, age, gender = line.strip().split(',')
self.students.append(Student(name, int(age), gender))
class StudentManagerGUI:
def __init__(self):
self.window = tk.Tk()
self.window.title('学生管理系统')
self.window.geometry('400x300')
self.name_label = tk.Label(self.window, text='姓名')
self.name_label.grid(row=0, column=0)
self.name_entry = tk.Entry(self.window)
self.name_entry.grid(row=0, column=1)
self.age_label = tk.Label(self.window, text='年龄')
self.age_label.grid(row=1, column=0)
self.age_entry = tk.Entry(self.window)
self.age_entry.grid(row=1, column=1)
self.gender_label = tk.Label(self.window, text='性别')
self.gender_label.grid(row=2, column=0)
self.gender_entry = tk.Entry(self.window)
self.gender_entry.grid(row=2, column=1)
self.add_button = tk.Button(self.window, text='添加', command=self.add_student)
self.add_button.grid(row=3, column=0)
self.update_button = tk.Button(self.window, text='修改', command=self.update_student)
self.update_button.grid(row=3, column=1)
self.delete_button = tk.Button(self.window, text='删除', command=self.delete_student)
self.delete_button.grid(row=3, column=2)
self.get_button = tk.Button(self.window, text='查看', command=self.get_student)
self.get_button.grid(row=4, column=0)
self.get_all_button = tk.Button(self.window, text='查看所有', command=self.get_all_students)
self.get_all_button.grid(row=4, column=1)
self.save_button = tk.Button(self.window, text='保存到文件', command=self.save_to_file)
self.save_button.grid(row=5, column=0)
self.load_button = tk.Button(self.window, text='从文件加载', command=self.load_from_file)
self.load_button.grid(row=5, column=1)
self.output_text = tk.Text(self.window)
self.output_text.grid(row=6, column=0, columnspan=3)
self.student_manager = StudentManager()
def add_student(self):
name = self.name_entry.get()
age = int(self.age_entry.get())
gender = self.gender_entry.get()
student = Student(name, age, gender)
self.student_manager.add_student(student)
self.output_text.insert(tk.END, f'添加学生:{name} {age} {gender}\n')
def update_student(self):
index = self.get_selected_index()
name = self.name_entry.get()
age = int(self.age_entry.get())
gender = self.gender_entry.get()
student = Student(name, age, gender)
self.student_manager.update_student(index, student)
self.output_text.insert(tk.END, f'修改学生:{name} {age} {gender}\n')
def delete_student(self):
index = self.get_selected_index()
student = self.student_manager.get_student(index)
self.student_manager.delete_student(index)
self.output_text.insert(tk.END, f'删除学生:{student.name} {student.age} {student.gender}\n')
def get_student(self):
index = self.get_selected_index()
student = self.student_manager.get_student(index)
self.output_text.insert(tk.END, f'查看学生:{student.name} {student.age} {student.gender}\n')
def get_all_students(self):
students = self.student_manager.get_all_students()
self.output_text.insert(tk.END, '所有学生:\n')
for student in students:
self.output_text.insert(tk.END, f'{student.name} {student.age} {student.gender}\n')
def save_to_file(self):
filename = tk.filedialog.asksaveasfilename(defaultextension='.txt')
self.student_manager.save_to_file(filename)
self.output_text.insert(tk.END, f'保存到文件:{filename}\n')
def load_from_file(self):
filename = tk.filedialog.askopenfilename(defaultextension='.txt')
self.student_manager.load_from_file(filename)
self.output_text.insert(tk.END, f'从文件加载:{filename}\n')
def get_selected_index(self):
return int(self.output_text.index(tk.INSERT).split('.')[0]) - 1
def run(self):
self.window.mainloop()
if __name__ == '__main__':
gui = StudentManagerGUI()
gui.run()
```
注意,上面的代码中使用了 `tk.filedialog` 来打开保存/加载文件的对话框,这需要您安装 Tkinter 模块。在 Windows 上,可以通过在命令行中运行 `python -m ensurepip` 安装 pip,然后运行 `pip install tkinter` 安装 Tkinter。在 Linux 上,可以使用系统包管理器来安装 tkinter。
阅读全文