0

In the below sample code, I have a simple GUI using Pyside6 which shows a total of 5 labels created with a for loop.

How do I assign a unique label name for each label that is being created so that later I can reference each specific label individually.

import sys
from PySide6 import QtWidgets


class MyWidget(QtWidgets.QWidget):
    def __init__(self):
        super().__init__()

        self.layout = QtWidgets.QGridLayout(self)
        

        for i in range(5):

            self.label = QtWidgets.QLabel(self)
            self.label.id_number = i
            self.layout.addWidget(self.label, i, 0)
            self.label.setText(str(i))
            self.label.setObjectName(u"label")
           

if __name__ == "__main__":
    app = QtWidgets.QApplication([])

    widget = MyWidget()
    widget.resize(800, 600)
    widget.show()

    sys.exit(app.exec_())

0 Answers0