Qt designer设计上位机使用button按钮读取excel文件并显示

毕设记录,代码小白,有什么请指教
效果:Qt designer设计上位机使用button按钮读取excel文件并显示_第1张图片

第一步:在qt5中设计自己需要的模块,然后将ui文件转换成py文件
[https://editor.csdn.net/md/?articleId=113793420](我的上一篇博文)
第二步:转换后,对代码进行修改,这里一篇网上大佬的代码我引用了,并根据自己的实际情况进行修改。详细情况可以参考这里

from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtGui import QIcon
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
import pandas as pd
import numpy as np


class Ui_MainWindow(QMainWindow):

    def __init__(self):
        super(QtWidgets.QMainWindow,self).__init__()
        self.setupUi(self)
        self.retranslateUi(self)

    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        #MainWindow.setEnabled(True)
        MainWindow.resize(570, 492)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        
        self.label = QtWidgets.QLabel(self.centralwidget)
        self.label.setGeometry(QtCore.QRect(180, 0, 161, 42))
        self.label.setObjectName("label")
        self.label_2 = QtWidgets.QLabel(self.centralwidget)
        self.label_2.setGeometry(QtCore.QRect(20, 10, 31, 171))
        self.label_2.setObjectName("label_2")
        
        self.groupBox = QtWidgets.QGroupBox(self.centralwidget)
        self.groupBox.setGeometry(QtCore.QRect(0, 290, 571, 171))
        self.groupBox.setObjectName("groupBox")


        self.tabWidget = QtWidgets.QTabWidget(self.centralwidget)
        self.tabWidget.setGeometry(QtCore.QRect(420, 50, 127, 80))
        self.tabWidget.setObjectName("tabWidget")
        self.tab = QtWidgets.QWidget()
        self.tab.setObjectName("tab")
        self.tabWidget.addTab(self.tab, "")
        self.tab_2 = QtWidgets.QWidget()
        self.tab_2.setObjectName("tab_2")
        self.tabWidget.addTab(self.tab_2, "")
        self.lcdNumber = QtWidgets.QLCDNumber(self.centralwidget)
        self.lcdNumber.setGeometry(QtCore.QRect(420, 140, 64, 23))
        self.lcdNumber.setObjectName("lcdNumber")
        self.lcdNumber_2 = QtWidgets.QLCDNumber(self.centralwidget)
        self.lcdNumber_2.setGeometry(QtCore.QRect(420, 170, 64, 23))
        self.lcdNumber_2.setObjectName("lcdNumber_2")

        self.tableWidget = QtWidgets.QTableWidget(self.centralwidget)
        self.tableWidget.setGeometry(QtCore.QRect(55, 50, 361, 231))
        self.tableWidget.setObjectName("tableWidget")
        self.tableWidget.setColumnCount(0)
        self.tableWidget.setRowCount(0)
        self.tableWidget.setStyleSheet("selection-background-color:pink")
        self.tableWidget.setEditTriggers(QAbstractItemView.NoEditTriggers)
        self.tableWidget.raise_()


        self.pushButton = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton.setGeometry(QtCore.QRect(420, 260, 75, 23))
        self.pushButton.setObjectName("pushButton")
        self.pushButton.setText("打开")
        MainWindow.setCentralWidget(self.centralwidget)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)
        self.retranslateUi(MainWindow)  #显示图表
        #读取文件函数复用
        self.pushButton.clicked.connect(self.openfile)
        self.pushButton.clicked.connect(self.creat_table_show)

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("mainWindow", "Wind Speed Prediction"))
        self.label.setText(_translate("mainWindow", "

短期风速预测

"
)) self.groupBox.setTitle(_translate("mainWindow", "数据图表")) self.tabWidget.setTabText(self.tabWidget.indexOf(self.tab), _translate("mainWindow", "Tab 1")) self.tabWidget.setTabText(self.tabWidget.indexOf(self.tab_2), _translate("mainWindow", "Tab 2")) self.pushButton.setText(_translate("mainWindow", "打开")) self.label_2.setText(_translate("mainWindow", "

"
)) def openfile(self): ###获取路径=================================================================== openfile_name = QFileDialog.getOpenFileName(self,'选择文件','','Excel files(*.xlsx , *.xls)') #print(openfile_name) global path_openfile_name ###获取路径==================================================================== path_openfile_name = openfile_name[0] def creat_table_show(self): ###===========读取表格,转换表格,=========================================== if len(path_openfile_name) > 0: input_table = pd.read_excel(path_openfile_name) #print(input_table) input_table_rows = input_table.shape[0] input_table_colunms = input_table.shape[1] #print(input_table_rows) #print(input_table_colunms) input_table_header = input_table.columns.values.tolist() #print(input_table_header) ###===========读取表格,转换表格,============================================ ###======================给tablewidget设置行列表头============================ self.tableWidget.setColumnCount(input_table_colunms) self.tableWidget.setRowCount(input_table_rows) self.tableWidget.setHorizontalHeaderLabels(input_table_header) ###======================给tablewidget设置行列表头============================ ###================遍历表格每个元素,同时添加到tablewidget中======================== for i in range(input_table_rows): input_table_rows_values = input_table.iloc[[i]] #print(input_table_rows_values) input_table_rows_values_array = np.array(input_table_rows_values) input_table_rows_values_list = input_table_rows_values_array.tolist()[0] #print(input_table_rows_values_list) for j in range(input_table_colunms): input_table_items_list = input_table_rows_values_list[j] #print(input_table_items_list) # print(type(input_table_items_list)) ###==============将遍历的元素添加到tablewidget中并显示======================= input_table_items = str(input_table_items_list) newItem = QTableWidgetItem(input_table_items) newItem.setTextAlignment(Qt.AlignHCenter|Qt.AlignVCenter) self.tableWidget.setItem(i, j, newItem) ###================遍历表格每个元素,同时添加到tablewidget中======================== else: print('ok') self.centralwidget.show() print('fail__2') if __name__ == "__main__": import sys app = QtWidgets.QApplication(sys.argv) MainWindow = QtWidgets.QMainWindow() ui = Ui_MainWindow() ui.setupUi(MainWindow) MainWindow.show() sys.exit(app.exec_()) ```python

你可能感兴趣的:(界面显示,python)