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

Viewed 116683

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)) so it will pass the value to the function:

def calluser(name):
    print name

def Qbutton():
    button = QtGui.QPushButton("button",widget)
    name = "user"
    button.setGeometry(100,100, 60, 35)
    button.connect(button, QtCore.SIGNAL('clicked()'), calluser(name))

One more thing, buttons will be generated using for loop; so name value will vary. So I want to attach each name with the button. I have done same thing in Pytk by using for loop and calling the argument base function when clicked.

7 Answers

You can simply write

name = "user"
button.clicked.connect(lambda: calluser(name))

You can do with parameters as well,

button.cliked.connect(lambda self.change_value( "extra"))

the function

def change_value( extraVariable):
     print( extraVariable)
Related