How to pass arguments to functions by the click of button in PyQt?

后端 未结 6 1490
时光说笑
时光说笑 2020-12-07 20:45

I want to pass the arguments to a function when I click the button. What should I add to this line button.connect(button, QtCore.SIGNAL(\'clicked()\'), calluser(name))

6条回答
  •  抹茶落季
    2020-12-07 21:26

    Here is another way. --- PARTIAL -- I find this one most simple:

        widget = QWidget()
        widgetLayout = QVBoxLayout()
    
        for action in list:
    
            button = QPushButton("{action}".format(action=action['name']),self)
            button.clicked.connect(partial(self.action_selected,action=action['name']))
            widgetLayout.addWidget(button)
    
        widget.setLayout(widgetLayout)
    
    def action_selected(self,action):
        print action
    

    found on: http://tech-artists.org/forum/showthread.php?3118-PyQt-Maya-How-to-pass-arguments-to-a-function-when-connecting-it-to-PyQt-button

提交回复
热议问题