Calling a function upon button press

Qt signals can have arguments that will be passed to the slots they are connected to; an example would be the new value as an argument in a signal changed. Therefore, while you can have a slot with arguments, you cannot define the actual values of those when connecting the signal to the slot, as they will be defined when emitting signal.

For defining an argument at connect time, you can use an additional function which does nothing but calling the original function with the defined argument:

def wrapper():
    funcion(12)

def funcion(a):
    print "Hola mundo" + str(a)

[...]
btn_brow_4.clicked.connect(wrapper)

As a side note: wrapper does not use braces here: the function is not called, but simply passed as an argument to the function connect. In your code, you called your function funcion, which returns nothing (=None), which was passed to connect in your original code, resulting in the error message you received.

To make that a bit cleaner, you can also use an anonymous function:

btn_brow_4.clicked.connect(lambda: funcion(12))

Note that Qt also provides ways of doing this, but (at least for me) the Python variants are easier to read.

Edit: Some more information: http://eli.thegreenplace.net/2011/04/25/passing-extra-arguments-to-pyqt-slot/

Leave a Comment