简体   繁体   中英

How to create Dynamic Buttons in Pyqt5

How can I handle each button separately? when clicking the assign button then the name will be assigned, what I have tried was trying to put each button and the name in a list but how can I keep track if the buttons were clicked?

Here is part of the code


 ## repeated for every worker
    for i in range(2):

        self.Vl_name_location = QtWidgets.QVBoxLayout()
        self.Vl_name_location.setObjectName("Vl_name_location")
        self.worker_name = QtWidgets.QLabel(self.groupBox)
        self.worker_name.setAlignment(QtCore.Qt.AlignCenter)

        ## from db
        self.worker_name.setText("worker_name")
        self.worker_name.setObjectName("worker_name")                       
        self.Vl_name_location.addWidget(self.worker_name)
        self.worker_location = QtWidgets.QLabel(self.groupBox)
        self.worker_location.setAlignment(QtCore.Qt.AlignCenter)
        self.worker_location.setObjectName("worker_location")
        # from db
        self.worker_location.setText("Eng,Zone B")
        self.Vl_name_location.addWidget(self.worker_location)
        self.Hl_worker.addLayout(self.Vl_name_location)
        #####
        ### assign button to connect the name of the worker to the image       on the db
        #####

        self.assign_button = QtWidgets.QPushButton(self.groupBox)
        self.assign_button.setMinimumSize(QtCore.QSize(50, 25))
          self.assign_button.setCursor(QtGui.QCursor(QtCore.Qt.PointingHandCursor))
        self.assign_button.setStyleSheet("")
        self.assign_button.setObjectName("assign_button")
        self.assign_button.setText( "Assign" )
        btns.append(self.assign_button)  
        self.Hl_worker.addWidget(self.assign_button)

preview of GUI

I'm not sure if I understood your question correctly, but I assume that when one of the two buttons is pressed, you want to set the text of the corresponding worker_name label. If this is the case, then one way to achieve this is to add the buttons and labels as keys and values to a dictionary, and use self.dct[self.sender()] to extract the label when the button is pressed, eg

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

        # dictionary for storing button-label pairs
        self.label_dct = {}
        self.add_workers()

    def add_workers(self):
        for i in range(10):
            worker_label = QtWidgets.QLabel('worker name')
            assign_button = QtWidgets.QPushButton('Assign')
            self.label_dct[assign_button] = worker_label

            # rest of setup

            assign_button.clicked.connect(self.set_label)

    @pyqtSlot()
    def set_label(self):
        button = self.sender()
        label = self.label_dct[button]
        label.setText("John Doe")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM