import sys from qdarkstyle import load_stylesheet_pyqt5 from PyQt5.QtWidgets import QApplication, QTableView from PyQt5.QtCore import QAbstractTableModel, Qt class QtTable(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 def render(df): app = QApplication(sys.argv) model = QtTable(df) view = QTableView() app.setStyleSheet(load_stylesheet_pyqt5()) fnt = view.font() fnt.setPointSize(9) view.setFont(fnt) view.setModel(model) view.setWindowTitle('viewer') view.resize(1080, 400) view.show() sys.exit(app.exec_())
如果想用PyQt5来即时显示pandas Dataframe的数据,直接call render这个function即可。
render(df)
完成!