Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why Does My GUI Keep Crashing when I Press the RadioButton (PYQT5)

I created a GUI (below) where it prints out a dataframe, I want the user to be able to filter the view of the dataframe based on the button they press. For example, if the user presses the Filter radio button (which filters the dataframe based off of the column C value), I want the GUI to display this new filtered dataframe on the GUI.

However, when I press the Filter Button (RadioButton) on the GUI, my GUI/program crashes. I know the filtered portion works, but how can I allow my filtered dataframe to be properly displayed on the GUI without crashing?

Console Message I get: Process finished with exit code -1073740791 (0xC0000409)

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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



>Solution :

The method Selected_Values expects two arguments, specifically a selected and a df arguments.

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

But you’re just passing one:

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

To fix this, simply provide the missing argument:

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

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading