我有一个GUI应用程序,它将csv文件加载为QtableView中的数据帧,并对数据执行一些功能,其中一个功能是提取标题并将其存储在带有复选框或一组复选框的QlistWidget中。到目前为止,我只能打印标题值。
代码:
from PyQt5 import QtCore, QtGui, QtWidgets
import pandas as pd
from PandasModel import PandasModel
import seaborn as sns
import cufflinks as cf
import plotly
import plotly.offline as py
import plotly.express as px
import plotly.graph_objs as go
from plotly.offline.offline import iplot
cf.go_offline()
cf.set_config_file(offline=False, world_readable=True)
class Widget(QtWidgets.QWidget):
def __init__(self, parent=None):
QtWidgets.QWidget.__init__(self, parent=None)
self.gridLayout_3 = QtWidgets.QGridLayout(self)
self.gridLayout_3.setObjectName("gridLayout_3")
self.gridLayout_2 = QtWidgets.QGridLayout()
self.gridLayout_2.setObjectName("gridLayout_2")
self.gridLayout = QtWidgets.QGridLayout()
self.gridLayout.setObjectName("gridLayout")
self.pathLE = QtWidgets.QLineEdit(self)
self.gridLayout_2.addWidget(self.pathLE, 0, 1, 1, 1)
self.loadBtn = QtWidgets.QPushButton("Select File", self)
self.loadBtn.clicked.connect(self.loadFile)
self.gridLayout_2.addWidget(self.loadBtn, 0, 0, 1, 1)
self.gridLayout_3.addLayout(self.gridLayout_2, 0, 0, 1, 4)
self.displayHeader = QtWidgets.QPushButton("Display header", self)
self.displayHeader.clicked.connect(lambda: self.print_df_header(self.df))
self.gridLayout.addWidget(self.displayHeader, 2, 0, 1, 1)
self.pandasTv = QtWidgets.QTableView(self)
self.verticalLayout.addWidget(self.pandasTv)
self.pandasTv.setSortingEnabled(True)
def loadFile(self):
fileName, _ = QtWidgets.QFileDialog.getOpenFileName(self, "Open File", "", "CSV Files (*.csv)");
self.pathLE.setText(fileName)
df = pd.read_csv(fileName)
model = PandasModel(df)
self.pandasTv.setModel(model)
self.df = df
def print_df_header(self,df):
print(self.df.columns.tolist())
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
w = Widget()
w.show()
sys.exit(app.exec_())
希望这有帮助:
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(800, 600)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.listWidget = QtWidgets.QListWidget(self.centralwidget)
self.listWidget.setGeometry(QtCore.QRect(170, 100, 256, 192))
self.listWidget.setObjectName("listWidget")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 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.newCheckBox=QtWidgets.QCheckBox(self.centralwidget)
header=QtWidgets.QListWidgetItem()
self.listWidget.addItem(header)
self.listWidget.addItem("Data")
header.setCheckState(1)
header.setText("Header")
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
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_())
我发现问题的解决方案非常简单,我为每个项目添加了一个复选框。
def print_df_header(self,df):
savelist = list(self.df)
for item in savelist:
qitem = QtWidgets.QListWidgetItem ( )
qitem.setText ( item )
qitem.setCheckState ( QtCore.Qt.Unchecked )
self.header_list.addItem ( qitem )
print(list(self.df))