已知一个班同学的平时成绩和考试成绩数据表(详见文件),请统计处该班级学生的作业平均分和考试平均分,并将平均分绘制成曲线图,并在界面上输出。
要求:点击界面按钮,读入文件数据,并输出图形。(说明:读取的文件格式可以自行改变,可以使用文本文件)
考核:
1、文件读写基本技能。
2、基本的窗口界面、图形界面编程。
btn = QPushButton(self)
btn.setText("打开文件")
btn.resize(120, 30)
btn.move(130, 0)
btn.setStyleSheet('background-color:green;font-size:20px;')
btn2 = QPushButton(self)
btn2.setText("分析数据")
btn2.resize(120, 30)
btn2.move(350, 0)
btn2.setStyleSheet('background-color:green;font-size:20px;')
# 用于显示表格
self.model = QStandardItemModel(41, 5)
self.tableview = QTableView(self)
# 关联QTableView控件和Model
self.tableview.setModel(self.model)
self.tableview.move(50, 30)
self.tableview.resize(500, 400)
self.label = QLabel(self)
self.label.move(50, 30)
self.label.resize(500, 400)
btn.clicked.connect(self.open_xlsx)
xlsx_name, _ = QFileDialog.getOpenFileName(None, "Open Excel File", "", "*.xlsx")
print(xlsx_name, type(xlsx_name))
# 创建对象
df = pd.read_excel(xlsx_name, sheet_name='Sheet1')
nrows = df.shape[0] # 行
ncols = df.columns.size # 列
print(nrows, ncols)
for i in range(nrows):
stu = {'id': '', 'name': '', 'phone': '', 'nomal_score': '', 'final_score': ''}
stu['id'] = df.iloc[i, 1]
stu['name'] = df.iloc[i, 2]
stu['phone'] = df.iloc[i, 3]
stu['final_score'] = df.iloc[i, 17]
sum = 0
for j in range(4, 17):
sum += df.iloc[i, j]
sum = sum / 13
stu['nomal_score'] = sum
stu_list.append(stu)
self.model.setHorizontalHeaderLabels(['学号', '姓名', '电话', '平时成绩', '期末成绩'])
# 添加数据
for i in range(0, 41):
id = stu_list[i]['id']
name = stu_list[i]['name']
phone = stu_list[i]['phone']
nomal_score = round(stu_list[i]['nomal_score'], 2)
final_score = round(stu_list[i]['final_score'], 2)
y1.append(nomal_score)
y2.append(final_score)
item1 = QStandardItem(str(id))
item2 = QStandardItem(name)
item3 = QStandardItem(str(phone))
item4 = QStandardItem(str(nomal_score))
item5 = QStandardItem(str(final_score))
self.model.setItem(i, 0, item1)
self.model.setItem(i, 1, item2)
self.model.setItem(i, 2, item3)
self.model.setItem(i, 3, item4)
self.model.setItem(i, 4, item5)
btn2.clicked.connect(self.fun2)
# 解决中文显示问题
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False
x = list(range(0,41))
fig = plt.figure(figsize=(7, 5)) # 新建画布
# plt.bar(x, y1, lw=4, color='b', label='平时成绩')
# plt.bar(x, y2, lw=4, color='r', label='期末成绩')
plt.plot(x, y1, lw=4, color='b', label='平时成绩')
plt.plot(x, y2, lw=4, color='r', label='期末成绩')
plt.legend(labels=["平时成绩", "期末成绩"], loc="upper left", fontsize=6)
plt.title('成绩数据表')
print('fun2')
# show出图形
#plt.show()
# 保存图片
fig.savefig("score")
pixmap = QPixmap("score.png")
pixmap = pixmap.scaled(500, 400)
#self.tableview.setPixmap(pixmap)
self.label.setPixmap(pixmap)
from PyQt5.Qt import *
import sys
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
stu_list = []
y1 = []
y2 = []
class Window(QWidget):
def __init__(self):
super().__init__() # 调用父类QWidget中的init方法
self.setWindowTitle("软件名称") # 调用自身的属性和方法
self.resize(600, 500)
self.func_list()
def func_list(self):
self.func()
def func(self):
btn = QPushButton(self)
btn.setText("打开文件")
btn.resize(120, 30)
btn.move(130, 0)
btn.setStyleSheet('background-color:green;font-size:20px;')
btn2 = QPushButton(self)
btn2.setText("分析数据")
btn2.resize(120, 30)
btn2.move(350, 0)
btn2.setStyleSheet('background-color:green;font-size:20px;')
self.model = QStandardItemModel(41, 5)
self.tableview = QTableView(self)
# 关联QTableView控件和Model
self.tableview.setModel(self.model)
self.tableview.move(50, 30)
self.tableview.resize(500, 400)
self.label = QLabel(self)
self.label.move(50, 30)
self.label.resize(500, 400)
btn.clicked.connect(self.open_xlsx)
btn2.clicked.connect(self.fun2)
def fun2(self):
# 解决中文显示问题
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False
x = list(range(0,41))
fig = plt.figure(figsize=(7, 5)) # 新建画布
# plt.bar(x, y1, lw=4, color='b', label='平时成绩')
# plt.bar(x, y2, lw=4, color='r', label='期末成绩')
plt.plot(x, y1, lw=4, color='b', label='平时成绩')
plt.plot(x, y2, lw=4, color='r', label='期末成绩')
plt.legend(labels=["平时成绩", "期末成绩"], loc="upper left", fontsize=6)
plt.title('成绩数据表')
print('fun2')
# show出图形
#plt.show()
# 保存图片
fig.savefig("score")
pixmap = QPixmap("score.png")
pixmap = pixmap.scaled(500, 400)
#self.tableview.setPixmap(pixmap)
self.label.setPixmap(pixmap)
def open_xlsx(self):
xlsx_name, _ = QFileDialog.getOpenFileName(None, "Open Excel File", "", "*.xlsx")
print(xlsx_name, type(xlsx_name))
# 创建对象
df = pd.read_excel(xlsx_name, sheet_name='Sheet1')
nrows = df.shape[0] # 行
ncols = df.columns.size # 列
print(nrows, ncols)
for i in range(nrows):
stu = {'id': '', 'name': '', 'phone': '', 'nomal_score': '', 'final_score': ''}
stu['id'] = df.iloc[i, 1]
stu['name'] = df.iloc[i, 2]
stu['phone'] = df.iloc[i, 3]
stu['final_score'] = df.iloc[i, 17]
sum = 0
for j in range(4, 17):
sum += df.iloc[i, j]
sum = sum / 13
stu['nomal_score'] = sum
stu_list.append(stu)
# input_table_header = df.columns.values.tolist() # 获取表头
self.model.setHorizontalHeaderLabels(['学号', '姓名', '电话', '平时成绩', '期末成绩'])
# 添加数据
for i in range(0, 41):
id = stu_list[i]['id']
name = stu_list[i]['name']
phone = stu_list[i]['phone']
nomal_score = round(stu_list[i]['nomal_score'], 2)
final_score = round(stu_list[i]['final_score'], 2)
y1.append(nomal_score)
y2.append(final_score)
item1 = QStandardItem(str(id))
item2 = QStandardItem(name)
item3 = QStandardItem(str(phone))
item4 = QStandardItem(str(nomal_score))
item5 = QStandardItem(str(final_score))
self.model.setItem(i, 0, item1)
self.model.setItem(i, 1, item2)
self.model.setItem(i, 2, item3)
self.model.setItem(i, 3, item4)
self.model.setItem(i, 4, item5)
if __name__ == '__main__':
app = QApplication(sys.argv) # 创建一个应用程序对象
# sys.argv可以接收用户命令行启动时所输入的参数,根据参数执行不同程序
# qApp 为全局对象
window = Window()
window.show()
sys.exit(app.exec_()) # 0是正常退出
# app.exec_() 进行循环
# sys.exit() 检测退出原因