Python 在pyqt5中单击按钮时显示数据帧
我是python和pyqt5的初学者。我试图在单击按钮时填充tableView。单击按钮,我想调用另一个类,它可以在传递数据帧时生成表。到目前为止,代码只显示空的tableviewPython 在pyqt5中单击按钮时显示数据帧,python,pandas,pyqt5,desktop-application,Python,Pandas,Pyqt5,Desktop Application,我是python和pyqt5的初学者。我试图在单击按钮时填充tableView。单击按钮,我想调用另一个类,它可以在传递数据帧时生成表。到目前为止,代码只显示空的tableview from PyQt5 import QtCore, QtGui, QtWidgets import sys import pandas as pd from PyQt5.QtWidgets import QApplication, QTableView from PyQt5.QtCore import QAbstra
from PyQt5 import QtCore, QtGui, QtWidgets
import sys
import pandas as pd
from PyQt5.QtWidgets import QApplication, QTableView
from PyQt5.QtCore import QAbstractTableModel, Qt
df=pd.read_csv("testing_tweets.csv" )
数据帧类:
class pandasModel(QAbstractTableModel):
def __init__(self, data):
QAbstractTableModel.__init__(self)
self._data = data
print("in")
def rowCount(self, parent=None):
return self._data.shape[0]
print("in")
def columnCount(self, parent=None):
return self._data.shape[1]
print("in")
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("testing_tweets.csv" )
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(640, 480)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.tableView = QtWidgets.QTableView(self.centralwidget)
self.tableView.setGeometry(QtCore.QRect(10, 10, 621, 331))
self.tableView.setObjectName("tableView")
self.pushButton = QtWidgets.QPushButton(self.centralwidget)
self.pushButton.setGeometry(QtCore.QRect(20, 350, 75, 23))
self.pushButton.setObjectName("pushButton")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 640, 21))
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)
self.pushButton.clicked.connect(self.extract)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.pushButton.setText(_translate("MainWindow", "PushButton"))
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_())
任何帮助都将不胜感激 我建议您阅读重复答案的答案,以便理解错误,因为这一次您将提供一个直接的解决方案:,在其他情况下,我希望您进行更深入的调查,而不仅仅是原谅自己是初学者谢谢您的帮助。但是,来自给定链接的代码不显示任何输出。将
testing_tweets.csv
更改为fullpath文件位于同一目录下,但文件的相对路径不是相对于.py,而是相对于启动应用程序的路径,以排除路径错误,如果错误仍然存在,我将分析问题所在,否则,我将指示创建绝对路径所需的代码。