简体   繁体   English

如何在 Pyqt5 中创建动态按钮

[英]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 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.我不确定我是否正确理解了您的问题,但我假设当按下两个按钮之一时,您想要设置相应worker_name标签的文本。 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如果是这种情况,那么实现此目的的一种方法是将按钮和标签作为键和值添加到字典中,并在按下按钮时使用self.dct[self.sender()]提取标签,例如

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")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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