vendredi 2 octobre 2020

PyQT5 how to find checkbox created dynamicaly in loop by objectName and change its text?

SO I have code like below:

from PyQt5 import QtCore, QtGui, QtWidgets

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(1000, 1000)
        MainWindow.setMinimumSize(QtCore.QSize(1000, 1000))
        MainWindow.setMaximumSize(QtCore.QSize(1000, 1000))
        MainWindow.setWindowTitle("MainWindow")
        # ---
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        # ---
        self.verticalLayoutWidget = QtWidgets.QWidget(self.centralwidget)
        self.verticalLayoutWidget.setGeometry(QtCore.QRect(10, 160, 561, 781))
        self.verticalLayoutWidget.setObjectName("verticalLayoutWidget")
        self.filesLayout = QtWidgets.QVBoxLayout(self.verticalLayoutWidget)
        self.filesLayout.setContentsMargins(0, 0, 0, 0)
        self.filesLayout.setObjectName("filesLayout")
        # ---
        self.checkBox_2 = QtWidgets.QCheckBox(self.verticalLayoutWidget)
        self.checkBox_2.setObjectName("checkBox_2")
        self.filesLayout.addWidget(self.checkBox_2)
        # ---
        self.horizontalLayoutWidget = QtWidgets.QWidget(self.centralwidget)
        self.horizontalLayoutWidget.setGeometry(QtCore.QRect(10, 10, 821, 80))
        self.horizontalLayoutWidget.setObjectName("horizontalLayoutWidget")
        self.seasonLayout = QtWidgets.QHBoxLayout(self.horizontalLayoutWidget)
        self.seasonLayout.setContentsMargins(0, 0, 0, 0)
        self.seasonLayout.setObjectName("seasonLayout")
        # ---
        sPaths = []
        sNames = []
        seasonss = ['W20', 'S20']
        for sName in seasonss:

            sTemp = sName.split('\\')[-1]
            if len(sTemp) == 3:
                # print(sTemp)
                sPaths.append(sName)
                sNames.append(sTemp)
        print(sNames)
        print(sPaths)
        i = 0
        for liczba in sNames:
            self.checkBox = QtWidgets.QCheckBox(self.horizontalLayoutWidget)
            self.checkBox.setObjectName("checkBox_" + str(i))
            self.checkBox.setText(str(liczba))
            self.seasonLayout.addWidget(self.checkBox)
            i = i + 1
            print(self.checkBox.objectName())
        # --- Here I want to change text in objectName checkbox_0
        # ---
        # ---
        # ---
        self.hotelList = QtWidgets.QPlainTextEdit(self.centralwidget)
        self.hotelList.setEnabled(False)
        self.hotelList.setGeometry(QtCore.QRect(10, 100, 561, 51))
        self.hotelList.setPlainText("")
        self.hotelList.setObjectName("hotelList")
        # ---
        self.seasonsButton = QtWidgets.QPushButton(self.centralwidget)
        self.seasonsButton.setGeometry(QtCore.QRect(850, 20, 111, 41))
        self.seasonsButton.setObjectName("seasonsButton")
        self.seasonsButton.setText("Select seasons")
        # ---
        self.codesButton = QtWidgets.QPushButton(self.centralwidget)
        self.codesButton.setEnabled(False)
        self.codesButton.setGeometry(QtCore.QRect(590, 110, 75, 41))
        self.codesButton.setObjectName("codesButton")
        self.codesButton.setText("Select Codes")
        # ---
        self.removeButton = QtWidgets.QPushButton(self.centralwidget)
        self.removeButton.setEnabled(False)
        self.removeButton.setGeometry(QtCore.QRect(580, 280, 151, 51))
        self.removeButton.setObjectName("removeButton")
        self.removeButton.setText("Remove from selected files")
        # ---
        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtWidgets.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 1000, 21))
        self.menubar.setObjectName("menubar")
        MainWindow.setMenuBar(self.menubar)
        self.statusbar = QtWidgets.QStatusBar(MainWindow)
        self.statusbar.setObjectName("statusbar")
        MainWindow.setStatusBar(self.statusbar)

        QtCore.QMetaObject.connectSlotsByName(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_())

What I want to do is to change text on objectName called checkBox_0. I was reading some answers to use findchild and I came out with something like this:

child = self.findChild(QtWidgets.QCheckBox, "checkBox_0")
self.child.setText('AaA')

But when I run that app i get error:

AttributeError: 'Ui_MainWindow' object has no attribute 'findChild'

I would appreciate any help with this - what am I doing wrong ?

Thank You in advance.




Aucun commentaire:

Enregistrer un commentaire