单击按钮时显示熊猫数据框

我试图在单击按钮时显示一个DataFrame。单击按钮后,我想调用另一个类,该类可以在传递数据框时生成表。到目前为止,代码只显示了空的tableview。

from PyQt5 import QtCore, QtGui, QtWidgets
import pandas as pd
from PyQt5.QtWidgets import QApplication, QTableView
from PyQt5.QtCore import QAbstractTableModel, Qt
df = pd.read_csv("Invent_peup.csv",delimiter=';')
数据框类
class pandasModel(QAbstractTableModel):

    def __init__(self, data):
        QAbstractTableModel.__init__(self)
        self._data = data

    def rowCount(self, parent=None):
        return self._data.shape[0]

    def columnCount(self, parent=None):
        return self._data.shape[1]

    def data(self, index, role=Qt.DisplayRole):
        if index.isValid():
            if role == Qt.DisplayRole:
                return str(self._data.iloc[index.row(), index.column()])
        return None

    def headerData(self, col, orientation, role):
        if orientation == Qt.Horizontal and role == Qt.DisplayRole:
            return self._data.columns[col]
        return None
显示类
class Ui_MainWindow(object):
    def extract(self):
        df = pd.read_csv("Invent_peup.csv",delimiter=';')
        print(df)
        app = QApplication(sys.argv)
        model = pandasModel(df)
        view = QTableView()
        view.setModel(model)
        view.resize(800, 600)
        view.show()
        sys.exit(app.exec_())
     def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(907, 422)
        MainWindow.setStyleSheet("background-color: rgb(85, 255, 0);\n"
"background-color: rgb(85, 85, 127);")
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.toolButton = QtWidgets.QToolButton(self.centralwidget)
        self.toolButton.setGeometry(QtCore.QRect(0, -30, 911, 111))
        self.toolButton.setStyleSheet("background-image: url(:/newPrefix/inventory_background.jpg);")
        self.toolButton.setObjectName("toolButton")
        self.pushButton = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton.setGeometry(QtCore.QRect(30, 120, 821, 31))
        self.pushButton.setStyleSheet("color: rgb(0, 0, 0);\n"
"font: 14pt \"MS Shell Dlg 2\";\n"
";\n"
"background-color: rgb(255, 170, 0);")
        self.pushButton.setObjectName("pushButton")

"font: 14pt \"MS Shell Dlg 2\";\n"
";\n"
"background-color: rgb(255, 170, 0);")

        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtWidgets.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 907, 18))
        self.menubar.setObjectName("menubar")
        MainWindow.setMenuBar(self.menubar)
        self.statusbar = QtWidgets.QStatusBar(MainWindow)
        self.statusbar.setObjectName("statusbar")
        MainWindow.setStatusBar(self.statusbar)

        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)
        MainWindow.setTabOrder(self.pushButton, self.pushButton_2)

        self.pushButton.clicked.connect(self.extract)




    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
        self.toolButton.setText(_translate("MainWindow", "..."))
        self.pushButton.setText(_translate("MainWindow", "Inventaire des peuplements "))

import bam_rc


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_())

Separatly without button configuration, dataframe class works perfectly enter image description here

任何帮助,将不胜感激 !

评论