How to pass arguments to callback functions in PyQt

后端 未结 5 1512
臣服心动
臣服心动 2020-11-30 10:21

I have around 10 QAction (this number will vary in runtime) in a toolbar, which all will do same thing, but using different parameters. I am thinking to add parameter as an

5条回答
  •  醉酒成梦
    2020-11-30 11:07

    You could use a lambda function associated to the GUI control's slot to pass extra arguments to the method you want to execute.

    # Create the build button with its caption
    self.build_button = QPushButton('&Build Greeting', self)
    # Connect the button's clicked signal to AddControl
    self.build_button.clicked.connect(lambda: self.AddControl('fooData'))
    def AddControl(self, name):
        print name
    

    Source: snip2code - Using Lambda Function To Pass Extra Argument in PyQt4

提交回复
热议问题