0

ow do I mess up with widgets added dynamically? If I set a new object name in every loop how do I access them and set signals?

 class MainWindow(QMainWindow):
       def __init__(self):
           QMainWindow.__init__(self)
           self.ui = Ui_MainWindow()
           self.ui.setupUi(self)
           self.show()
           self.plainTextEdit.textChanged.connect(self.WordCounter)

    for x in range(1, 5): # for now it will make 4 word counter
        for y in range(1, 2):
            self.create_input_box(x, y)

def create_input_box(self, row_num, column_num):
    self.plainTextEdit = QPlainTextEdit(self.ui.gridLayoutWidget)
    self.plainTextEdit.setObjectName(u"plainTextEdit")
    self.plainTextEdit.setMaximumSize(QSize(629, 106))
    self.ui.gridLayout.addWidget(self.plainTextEdit, row_num, column_num, 1, 1)

    self.label = QLabel(self.ui.gridLayoutWidget)
    self.label.setObjectName(u"label")
    self.label.setText("what")
    self.ui.gridLayout.addWidget(self.label, row_num, column_num+1, 1, 1)

def WordCounter(self):
    a_string = self.plainTextEdit.toPlainText()
    word_list = a_string.split()
    number_of_words = len(word_list)
    self.label.setText(str(number_of_words))
eyllanesc
  • 221,139
  • 17
  • 121
  • 189
  • your question is unclear – eyllanesc Oct 01 '21 at 17:37
  • Use [setattr()](https://docs.python.org/3/library/functions.html#setattr) to create attributes from dynamically generated names: e.g. `setattr(self, f'label_{row_num}_{column_num}', label)`. You could also use a dict to hold the widgets, instead of using attributes. – ekhumoro Oct 01 '21 at 19:45

0 Answers0