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

uahmed picture uahmed · Jul 22, 2011 · Viewed 77.2k times · Source

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.

Answer

ozcanyarimdunya picture ozcanyarimdunya · Jul 23, 2019

You can simply write

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