python 为什么按单选按钮时GUI不断崩溃(PYQT5)

gopyfrb3  于 2023-01-16  发布在  Python
关注(0)|答案(1)|浏览(184)

我创建了一个GUI(如下图所示),它可以打印出 Dataframe ,我希望用户能够根据他们按下的按钮过滤 Dataframe 的视图。例如,如果用户按下Filter单选按钮(根据列C值过滤 Dataframe ),我希望GUI在GUI上显示这个新过滤的 Dataframe 。
但是,当我按下GUI上的Filter按钮(RadioButton)时,我的GUI/程序崩溃。我知道过滤后的部分可以工作,但我如何才能让过滤后的 Dataframe 正确显示在GUI上而不崩溃?
我收到的控制台消息:进程已完成,退出代码为-1073740791(0xC 0000409)

from PyQt5.QtCore import Qt, QSize, QAbstractTableModel
import os
import pandas as df
import numpy as np
import sys
import pandas.io.formats.style
from PyQt5 import QtGui, QtCore
df.set_option('display.max_columns', 25)
df.set_option('display.width', 2000)
from PyQt5.QtWidgets import QMainWindow, QPushButton, QListWidget, QPlainTextEdit, \
    QScrollBar, QWidget, QCheckBox, QHBoxLayout, QApplication, QSizePolicy, QVBoxLayout, QScrollArea, QTableView,QTextEdit, \
    QRadioButton

class MainWindow(QMainWindow):
    def __init__(self, parent = None):
        super().__init__(parent)
        self.setWindowTitle('Test GUI')
        self.setGeometry(0, 0, 1200, 650)
        self.CoreFunctionality()



    def Selected_Values(self,selected, df):
        if selected:
            print(df.loc[df['C'] == 50])

    #         # print("helloworld")

    def CoreFunctionality(self):
        hbox = QHBoxLayout()
        hbox.addStretch(1)

        # textEdit = QPlainTextEdit()

        randomDF = df.DataFrame(np.random.randint(0, 100, size=(100, 4)), columns=list('ABCD'))

        self.centralwidget = QWidget(self)
        self.pdtable = QTableView(self.centralwidget)
        self.model = PandasTableModel(randomDF)
        self.pdtable.setModel(self.model)
        self.setCentralWidget(self.centralwidget)
        self.pdtable.setGeometry(350, 100, 750, 500)
        # self.pdtable.move(50,10)
        self.pdtable.setShowGrid(False)
        self.pdtable.resizeColumnsToContents()
        hbox.addWidget(self.pdtable)

        vbox = QVBoxLayout()
        vbox.addStretch(1)
        vbox.addLayout(hbox)
        self.setLayout(vbox)
        self.pdtable.resize(800, 500)

        #Checkbox filter
        filterCheckbox = QRadioButton('Filter Button', self)
        filterCheckbox.move(20, 70)

        filterCheckbox.toggled.connect(lambda: self.Selected_Values(randomDF))


class PandasTableModel(QtGui.QStandardItemModel):
    def __init__(self, data, parent=None):
        QtGui.QStandardItemModel.__init__(self, parent)
        self._data = data
        for col in data.columns:
            data_col = [QtGui.QStandardItem("{}".format(x)) for x in data[col].values]
            self.appendColumn(data_col)
        return

    def rowCount(self, parent=None):
        return len(self._data.values)

    def columnCount(self, parent=None):
        return self._data.columns.size

    def headerData(self, x, orientation, role):
        if orientation == Qt.Horizontal and role == Qt.DisplayRole:
            return self._data.columns[x]
        if orientation == Qt.Vertical and role == Qt.DisplayRole:
            return self._data.index[x]
        return None

if (__name__ == '__main__'):
    application = QApplication([])
    MainWindow = MainWindow()

    MainWindow.show()
    application.exec()
qybjjes1

qybjjes11#

方法Selected_Values需要两个参数,具体来说是一个selected参数和一个df参数。

def Selected_Values(self, selected, df):
   # ...

但你只是路过一个:

filterCheckbox.toggled.connect(lambda: self.Selected_Values(randomDF))

要解决此问题,只需提供缺少的参数:

filterCheckbox.toggled.connect(lambda: self.Selected_Values(filterCheckbox.isChecked(), randomDF))

相关问题