from tkinter import *
import pandas as pd
import numpy as np
# 根据成绩计算等级
def level(grade):
if 90 <= grade <= 100:
return 'A'
elif 80 <= grade < 90:
return 'B'
elif 70 <= grade < 80:
return 'C'
elif 60 <= grade < 70:
return 'D'
else:
return 'E'
# 1.读入成绩并显示
def get_dis():
global dc
dc = pd.read_csv("note.dat", sep='\t', header=0)
text.delete(0.0, 'end')
text.insert('end', dc)
# 2.计算总评成绩和等级,并添加到dc的新列
def compute():
dc['总评成绩'] = 0.3 * dc['平时成绩'] + 0.3 * dc['期中考试成绩'] + 0.4 * dc['期末考试成绩']
level_vector = np.vectorize(level) # 对level进行向量化处理
dc['等级'] = level_vector(dc['总评成绩'])
text.delete(0.0, 'end')
text.insert('end', "总评成绩和等级计算成功!")
# 3.显示总评成绩和等级
def dis_fin_le():
global out_dat
out_dat = dc.loc[:, ['学号', '总评成绩', '等级']] # loc通过具体索引切片,iloc通过行列号切片
text.delete(0.0, 'end')
text.insert('end', out_dat)
# 4.计算总平均成绩并显示
def print_mean():
text.delete(0.0, 'end')
text.insert('end', "总平均成绩为:" + "%.4f" % dc['总评成绩'].mean())
# 5.显示各等级人数及占比
def level_count():
a = dc['等级'].value_counts() # 计算各等级人数
b = dc['等级'].value_counts(normalize=True) # 计算各等级比例
b = b.apply(lambda x: '%.2f%%' % (x * 100)) # 转换为百分比显示
d = pd.DataFrame({'人数': a, '占比': b}) # 人数和占比组成DataFrame
d.index.name = "等级"
text.delete(0.0, 'end')
text.insert('end', "各个等级学生人数以及占总人数的百分比为:\n")
text.insert('end', d)
# 6.按等级筛选
def sort():
choice2 = text.get(0.0, 'end')[-2] # input("请输入等级以筛选(A,B,C,D或E),其他键将退出:")
if choice2 != 'A' and choice2 != 'B' and choice2 != 'C' and choice2 != 'D' and choice2 != 'E':
text.delete(0.0, 'end')
text.insert('end', "输入错误,请检查后重新输入!(范围:A,B,C,D,E)")
else:
text.delete(0.0, 'end')
text.insert('end', out_dat[out_dat['等级'] == choice2])
# 7.总评成绩和等级输出到文件
def output():
out_dat.to_csv("out.dat", sep='\t', mode='w+', index=0) # 不保留行索引
text.delete(0.0, 'end')
text.insert('end', "总评成绩和等级数据已成功写入到文件!\n文件名:out.dat")
win = Tk() # 创建窗口
win.title('学生成绩核算系统') # 设置标题
win.geometry('900x850')
label = Label(win, text="欢迎使用学生成绩核算系统", font=('Arial', 20), width=100, height=2)
btn1 = Button(win, text='1、读入学生成绩并显示 ', command=get_dis)
btn2 = Button(win, text='2、计算总评成绩和等级 ', command=compute)
btn3 = Button(win, text='3、显示总评成绩和等级 ', command=dis_fin_le)
btn4 = Button(win, text='4、计算总平均成绩并显示', command=print_mean)
btn5 = Button(win, text='5、显示各等级人数及占比', command=level_count)
btn6 = Button(win, text='6、在下方输入等级以筛选', command=sort)
btn7 = Button(win, text='7、输出总评成绩和等级 ', command=output)
btn8 = Button(win, text='8、退出本核算系统 ', command=win.quit)
text = Text(win) # 创建多行文本框,用于输入输出
label.pack()
btn1.pack()
btn2.pack()
btn3.pack()
btn4.pack()
btn5.pack()
btn6.pack()
btn7.pack()
btn8.pack()
text.pack()
win.mainloop() # 进入消息循环
这段代码有哪些错误?请改正
最新发布