簡體   English   中英

如何使用QCheckbox過濾QTableview DataFrame PYQT5

[英]How to Use QCheckbox to Filter QTableview DataFrame PYQT5

我有一個在 QTableView model 中使用的 dataframe。我試圖讓復選框過濾 dataframe / QTableView model。它基本上可以工作,但是我不確定如何構建我的代碼以使用多個復選框 not 過濾 dataframe只有一個。

例如,我想根據列值過濾 dataframe(單擊復選框,它會過濾數據框),但我也希望針對多個復選框過濾/顯示它。 我希望 dataframe 顯示大於 80 和小於 25 的結果。如有任何意見或建議,我將不勝感激。

代碼:

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 Selection(self,selected, f_df, df):
        if selected:


            self.model = PandasTableModel(f_df)
            self.pdtable.setModel(self.model)
            self.setCentralWidget(self.centralwidget)
            self.pdtable.setGeometry(350, 100, 750, 500)

        if selected == False:
            self.model = PandasTableModel(df)
            self.pdtable.setModel(self.model)
            self.setCentralWidget(self.centralwidget)
            self.pdtable.setGeometry(350, 100, 750, 500)

    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, 200, 500)
        # self.pdtable.move(50,10)
        self.pdtable.setShowGrid(False)
        self.pdtable.resizeColumnsToContents()
        hbox.addWidget(self.pdtable)

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

        #Checkbox filter
        filterCheckbox1 = QCheckBox('> 80', self)
        filterCheckbox1.move(20, 70)

        filterCheckbox2 = QCheckBox('< 25', self)
        filterCheckbox2.move(20, 90)

        filterCheckbox1.toggled.connect(lambda: self.Selection(filterCheckbox1.isChecked(), randomDF.loc[randomDF['C'] >= 80], randomDF))
        filterCheckbox2.toggled.connect(lambda: self.Selection(filterCheckbox2.isChecked(), randomDF.loc[randomDF['C'] <= 25], 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()


將這些代碼寫在我如何標記位置上

conditions = [] # above class MainWindow(QMainWindow):

過濾器 function

# convert this function => def Selection(self, selected, cond, d): and write below code in function
def filter_func(x):
    if not conditions:
        return x
    for c,v in conditions:
        if c == "gt":
            if x >= v:
                return x
        elif c == "lt":
            if x <= v:
                return x

並使用這些部分

if selected:
    conditions.append(func)            
    self.model = PandasTableModel(d.applymap(filter_func))

if selected == False:
    conditions.remove(func)
    self.model = PandasTableModel(d.applymap(filter_func))

並使用這些代碼

filterCheckbox1.toggled.connect(lambda: self.Selection(filterCheckbox1.isChecked(), ("gt",80), randomDF))
filterCheckbox2.toggled.connect(lambda: self.Selection(filterCheckbox2.isChecked(), ("lt",25), randomDF))

我希望對你有用:)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM