用python写出一段代码实现学生信息管理系统,可实现可视化操作,包括添加数据,修改数据,删除数据,查询数据,包括数学,语文,英语,物理,化学等学科,对输入数据制图,并分析是否偏科,给出学习建议,每一次添加完数据都清除输入的数据,输入数据按照示例来填写,错误填写会报错,数学语文英语分数在0-150,物理化学在0-100,添加的数据在下方以表格出现,可以在表格上实现数据的删除修改,代码加上注释。
时间: 2023-12-10 11:03:38 浏览: 72
好的,下面是一个基本的代码框架,你可以根据自己的需求进行修改和完善。
```python
import tkinter as tk
import tkinter.messagebox as messagebox
import matplotlib.pyplot as plt
# 定义一个用于存储学生信息的列表
students = []
# 定义一个用于存储学科信息的列表
subjects = ['数学', '语文', '英语', '物理', '化学']
# 创建主界面
root = tk.Tk()
root.title('学生信息管理系统')
# 定义一些辅助函数,用于实现各种功能
# 函数1:添加数据
def add_data():
# 获取输入框中的数据
name = name_var.get()
math_score = math_score_var.get()
chinese_score = chinese_score_var.get()
english_score = english_score_var.get()
physics_score = physics_score_var.get()
chemistry_score = chemistry_score_var.get()
# 检查输入数据是否合法
try:
math_score = float(math_score)
chinese_score = float(chinese_score)
english_score = float(english_score)
physics_score = float(physics_score)
chemistry_score = float(chemistry_score)
except ValueError:
messagebox.showerror('错误', '分数必须是数字')
return
if not (0 <= math_score <= 150 and 0 <= chinese_score <= 150 and 0 <= english_score <= 150
and 0 <= physics_score <= 100 and 0 <= chemistry_score <= 100):
messagebox.showerror('错误', '分数必须在有效范围内')
return
# 计算总分和平均分
total_score = math_score + chinese_score + english_score + physics_score + chemistry_score
average_score = total_score / 5
# 将数据添加到学生列表中
students.append({
'name': name,
'math_score': math_score,
'chinese_score': chinese_score,
'english_score': english_score,
'physics_score': physics_score,
'chemistry_score': chemistry_score,
'total_score': total_score,
'average_score': average_score
})
# 清空输入框中的数据
name_var.set('')
math_score_var.set('')
chinese_score_var.set('')
english_score_var.set('')
physics_score_var.set('')
chemistry_score_var.set('')
# 更新表格中的数据
update_table()
# 函数2:修改数据
def edit_data(row, col, new_value):
# 修改学生列表中的数据
students[row][subjects[col]] = new_value
# 更新表格中的数据
update_table()
# 函数3:删除数据
def delete_data(row):
# 从学生列表中删除指定的数据
students.pop(row)
# 更新表格中的数据
update_table()
# 函数4:查询数据
def query_data():
# 获取查询条件
subject = subject_var.get()
min_score = min_score_var.get()
max_score = max_score_var.get()
# 检查输入数据是否合法
try:
min_score = float(min_score)
max_score = float(max_score)
except ValueError:
messagebox.showerror('错误', '分数必须是数字')
return
if not (0 <= min_score <= max_score <= 150 or 0 <= min_score <= max_score <= 100):
messagebox.showerror('错误', '分数必须在有效范围内')
return
# 根据查询条件筛选学生列表中的数据
filtered_students = []
for student in students:
if student[subject] >= min_score and student[subject] <= max_score:
filtered_students.append(student)
# 显示查询结果
show_query_result(filtered_students)
# 函数5:制图并分析偏科情况
def analyze_data():
# 计算每个学生的标准差
stds = []
for student in students:
data = [student[subject] for subject in subjects]
std = sum([(x - sum(data) / len(data)) ** 2 for x in data]) ** 0.5 / len(data)
stds.append(std)
# 绘制标准差图表
plt.hist(stds, bins=10)
plt.xlabel('标准差')
plt.ylabel('人数')
plt.title('偏科情况分析')
plt.show()
# 分析偏科情况并给出建议
# TODO: 根据实际需求进行修改
# 函数6:更新表格中的数据
def update_table():
# 清空表格中的数据
for widget in table_frame.winfo_children():
widget.destroy()
# 显示表格头部
for i, subject in enumerate(subjects):
label = tk.Label(table_frame, text=subject)
label.grid(row=0, column=i+1)
# 显示表格内容
for i, student in enumerate(students):
# 显示学生姓名
name_label = tk.Label(table_frame, text=student['name'])
name_label.grid(row=i+1, column=0)
# 显示学生各个学科的成绩和平均分
for j, subject in enumerate(subjects):
score_label = tk.Label(table_frame, text=student[subject])
score_label.grid(row=i+1, column=j+1)
average_score_label = tk.Label(table_frame, text=student['average_score'])
average_score_label.grid(row=i+1, column=len(subjects)+1)
# 显示删除和修改按钮
delete_button = tk.Button(table_frame, text='删除', command=lambda row=i: delete_data(row))
delete_button.grid(row=i+1, column=len(subjects)+2)
edit_button = tk.Button(table_frame, text='修改', command=lambda row=i: edit_data_dialog(row))
edit_button.grid(row=i+1, column=len(subjects)+3)
# 函数7:显示查询结果
def show_query_result(filtered_students):
# 创建一个新的窗口,用于显示查询结果
query_result_window = tk.Toplevel(root)
query_result_window.title('查询结果')
# 显示表格头部
for i, subject in enumerate(subjects):
label = tk.Label(query_result_window, text=subject)
label.grid(row=0, column=i)
# 显示表格内容
for i, student in enumerate(filtered_students):
for j, subject in enumerate(subjects):
score_label = tk.Label(query_result_window, text=student[subject])
score_label.grid(row=i+1, column=j)
# 函数8:显示修改对话框
def edit_data_dialog(row):
# 创建一个新的窗口,用于显示修改对话框
edit_data_window = tk.Toplevel(root)
edit_data_window.title('修改数据')
# 显示学生姓名和各个学科的成绩
student = students[row]
for i, subject in enumerate(subjects):
label = tk.Label(edit_data_window, text=subject)
label.grid(row=i, column=0)
value = tk.StringVar(value=str(student[subject]))
entry = tk.Entry(edit_data_window, textvariable=value)
entry.grid(row=i, column=1)
def validate(new_value):
try:
new_value = float(new_value)
except ValueError:
return False
if not (0 <= new_value <= 150 and subject in ['数学', '语文', '英语'] or 0 <= new_value <= 100
and subject in ['物理', '化学']):
return False
return True
entry.config(validate='key', validatecommand=(edit_data_window.register(validate), '%P'))
# 显示确定和取消按钮
confirm_button = tk.Button(edit_data_window, text='确定', command=lambda: confirm_edit_data(row))
confirm_button.grid(row=len(subjects), column=0)
cancel_button = tk.Button(edit_data_window, text='取消', command=edit_data_window.destroy)
cancel_button.grid(row=len(subjects), column=1)
# 定义一个函数,用于确认修改数据
def confirm_edit_data(row):
new_values = [float(entry.get()) for entry in edit_data_window.grid_slaves() if isinstance(entry, tk.Entry)]
edit_data_window.destroy()
edit_data(row, slice(None), new_values)
# 创建输入框和按钮
input_frame = tk.Frame(root)
input_frame.pack(pady=10)
name_label = tk.Label(input_frame, text='姓名:')
name_label.grid(row=0, column=0)
name_var = tk.StringVar()
name_entry = tk.Entry(input_frame, textvariable=name_var)
name_entry.grid(row=0, column=1)
math_score_label = tk.Label(input_frame, text='数学成绩:')
math_score_label.grid(row=1, column=0)
math_score_var = tk.StringVar()
math_score_entry = tk.Entry(input_frame, textvariable=math_score_var)
math_score_entry.grid(row=1, column=1)
chinese_score_label = tk.Label(input_frame,
阅读全文