用python写一个学生管理系统内容包括,新增学生信息,修改学生信息,删除学生信息,查看所有学生信息,查看单个学生信息,使用gui中的grid布局,并且将学生信息存入 文本文档,界面要整洁美观
时间: 2024-03-09 22:46:17 浏览: 57
好的,这是一个比较复杂的问题,我尽量详细地回答。
先来看一下创建一个学生管理系统需要哪些步骤:
1. 设计学生信息的数据结构
2. 创建GUI界面
3. 编写新增、修改、删除、查看等功能的代码
4. 将学生信息存入文本文档
下面分别来看这些步骤。
1. 设计学生信息的数据结构
考虑到一个学生的信息包括姓名、年龄、性别、班级等,我们可以将其设计为一个字典,例如:
```python
student = {
"name": "张三",
"age": 18,
"gender": "男",
"class": "高一一班"
}
```
为了管理多个学生的信息,我们可以将学生信息存储在一个列表中:
```python
students = [
{
"name": "张三",
"age": 18,
"gender": "男",
"class": "高一一班"
},
{
"name": "李四",
"age": 17,
"gender": "女",
"class": "高一二班"
}
]
```
2. 创建GUI界面
这里我们使用Python的标准GUI库Tkinter来创建界面。我们可以使用grid布局来排列界面元素。以下是一个简单的示例代码:
```python
import tkinter as tk
window = tk.Tk()
window.title("学生管理系统")
# 创建标签和输入框
name_label = tk.Label(window, text="姓名")
name_label.grid(row=0, column=0)
name_entry = tk.Entry(window)
name_entry.grid(row=0, column=1)
age_label = tk.Label(window, text="年龄")
age_label.grid(row=1, column=0)
age_entry = tk.Entry(window)
age_entry.grid(row=1, column=1)
gender_label = tk.Label(window, text="性别")
gender_label.grid(row=2, column=0)
gender_entry = tk.Entry(window)
gender_entry.grid(row=2, column=1)
class_label = tk.Label(window, text="班级")
class_label.grid(row=3, column=0)
class_entry = tk.Entry(window)
class_entry.grid(row=3, column=1)
# 创建按钮
add_button = tk.Button(window, text="新增")
add_button.grid(row=4, column=0)
update_button = tk.Button(window, text="修改")
update_button.grid(row=4, column=1)
delete_button = tk.Button(window, text="删除")
delete_button.grid(row=5, column=0)
view_all_button = tk.Button(window, text="查看所有")
view_all_button.grid(row=5, column=1)
view_one_button = tk.Button(window, text="查看单个")
view_one_button.grid(row=6, column=0)
window.mainloop()
```
3. 编写新增、修改、删除、查看等功能的代码
我们可以为每个按钮绑定一个相应的函数来实现对学生信息的增删改查。以下是一个示例代码:
```python
import tkinter as tk
window = tk.Tk()
window.title("学生管理系统")
students = []
def add_student():
name = name_entry.get()
age = age_entry.get()
gender = gender_entry.get()
class_ = class_entry.get()
student = {
"name": name,
"age": age,
"gender": gender,
"class": class_
}
students.append(student)
save_students()
clear_entries()
def update_student():
pass
def delete_student():
pass
def view_all_students():
pass
def view_one_student():
pass
def clear_entries():
name_entry.delete(0, tk.END)
age_entry.delete(0, tk.END)
gender_entry.delete(0, tk.END)
class_entry.delete(0, tk.END)
def save_students():
with open("students.txt", "w") as f:
for student in students:
f.write(f"{student['name']},{student['age']},{student['gender']},{student['class']}\n")
# 创建标签和输入框
name_label = tk.Label(window, text="姓名")
name_label.grid(row=0, column=0)
name_entry = tk.Entry(window)
name_entry.grid(row=0, column=1)
age_label = tk.Label(window, text="年龄")
age_label.grid(row=1, column=0)
age_entry = tk.Entry(window)
age_entry.grid(row=1, column=1)
gender_label = tk.Label(window, text="性别")
gender_label.grid(row=2, column=0)
gender_entry = tk.Entry(window)
gender_entry.grid(row=2, column=1)
class_label = tk.Label(window, text="班级")
class_label.grid(row=3, column=0)
class_entry = tk.Entry(window)
class_entry.grid(row=3, column=1)
# 创建按钮
add_button = tk.Button(window, text="新增", command=add_student)
add_button.grid(row=4, column=0)
update_button = tk.Button(window, text="修改", command=update_student)
update_button.grid(row=4, column=1)
delete_button = tk.Button(window, text="删除", command=delete_student)
delete_button.grid(row=5, column=0)
view_all_button = tk.Button(window, text="查看所有", command=view_all_students)
view_all_button.grid(row=5, column=1)
view_one_button = tk.Button(window, text="查看单个", command=view_one_student)
view_one_button.grid(row=6, column=0)
window.mainloop()
```
4. 将学生信息存入文本文档
我们可以在每次新增、修改、删除学生信息时,将学生信息存入一个文本文档中。以下是一个示例代码:
```python
def save_students():
with open("students.txt", "w") as f:
for student in students:
f.write(f"{student['name']},{student['age']},{student['gender']},{student['class']}\n")
```
注意,在读取学生信息时,我们需要将存储在文本文档中的字符串转换为字典格式:
```python
def load_students():
with open("students.txt", "r") as f:
for line in f:
name, age, gender, class_ = line.strip().split(",")
student = {
"name": name,
"age": age,
"gender": gender,
"class": class_
}
students.append(student)
```
最后,将上述功能代码整合到GUI界面中即可完成一个简单的学生管理系统。完整代码如下:
```python
import tkinter as tk
window = tk.Tk()
window.title("学生管理系统")
students = []
def add_student():
name = name_entry.get()
age = age_entry.get()
gender = gender_entry.get()
class_ = class_entry.get()
student = {
"name": name,
"age": age,
"gender": gender,
"class": class_
}
students.append(student)
save_students()
clear_entries()
def update_student():
pass
def delete_student():
pass
def view_all_students():
pass
def view_one_student():
pass
def clear_entries():
name_entry.delete(0, tk.END)
age_entry.delete(0, tk.END)
gender_entry.delete(0, tk.END)
class_entry.delete(0, tk.END)
def save_students():
with open("students.txt", "w") as f:
for student in students:
f.write(f"{student['name']},{student['age']},{student['gender']},{student['class']}\n")
def load_students():
with open("students.txt", "r") as f:
for line in f:
name, age, gender, class_ = line.strip().split(",")
student = {
"name": name,
"age": age,
"gender": gender,
"class": class_
}
students.append(student)
load_students()
# 创建标签和输入框
name_label = tk.Label(window, text="姓名")
name_label.grid(row=0, column=0)
name_entry = tk.Entry(window)
name_entry.grid(row=0, column=1)
age_label = tk.Label(window, text="年龄")
age_label.grid(row=1, column=0)
age_entry = tk.Entry(window)
age_entry.grid(row=1, column=1)
gender_label = tk.Label(window, text="性别")
gender_label.grid(row=2, column=0)
gender_entry = tk.Entry(window)
gender_entry.grid(row=2, column=1)
class_label = tk.Label(window, text="班级")
class_label.grid(row=3, column=0)
class_entry = tk.Entry(window)
class_entry.grid(row=3, column=1)
# 创建按钮
add_button = tk.Button(window, text="新增", command=add_student)
add_button.grid(row=4, column=0)
update_button = tk.Button(window, text="修改", command=update_student)
update_button.grid(row=4, column=1)
delete_button = tk.Button(window, text="删除", command=delete_student)
delete_button.grid(row=5, column=0)
view_all_button = tk.Button(window, text="查看所有", command=view_all_students)
view_all_button.grid(row=5, column=1)
view_one_button = tk.Button(window, text="查看单个", command=view_one_student)
view_one_button.grid(row=6, column=0)
window.mainloop()
```
阅读全文