0

I am having some trouble passing arguments to a function using functools.partial() when connecting it to a GUI Qt signal (log_this() is a dummy function just to convey the problem):

self.ui.comboBox_1.currentIndexChanged.connect(functools.partial(self.log_this, val='aaaaaa'))

def log_this(self, val='0000'):
    log.info(val)

gotten error:

TypeError: log_this() got multiple values for argument 'val'

if I use lambda to call the function it works fine:

self.ui.comboBox_1.currentIndexChanged.connect(lambda: self.log_this(val='aaaaaa'))

looking into this, I see that log_this() is getting the current comboBox index. I also tried working with positional arguments and also gotten an error like the above.

I want to understand if it is correct to use functools.partial() or should I just stick to lambda on such cases. if correct, what is the right way to do it?

Adding more details:

my class:

class AppMainWindow(QtGui.QMainWindow):
    message = QtCore.Signal(str)

    def __init__(self, parent=None):
        super().__init__(parent=parent)
        self.manual_test = True
        self.all_tests_enabled = False
        self.set_connections()

    def set_connections(self): 
        self.ui.comboBox_1.currentIndexChanged.connect(
            functools.partial(self.log_this, val='aaaaaa'))
        self.ui.pushButton_1.clicked.connect(self.add_item)

    def log_this(self, val='0000'):
        log.info(val)

I am using PySide 1.2.2.

Thanks in advance!

Ido A
  • 87
  • 1
  • 1
  • 8
  • is your `log_this` actually a method of a class? If not, why does it have a `self`? If it is, can you show a more complete example? – The Compiler Apr 05 '17 at 03:18
  • yes it is part of my main window class. I've added an example of the class – Ido A Apr 05 '17 at 12:25

1 Answers1

0

This is happening because currentIndexChanged emits with a value (probably the new index), which your method gets as val. You have various options here:

  • Connecting it to something like lambda _idx: self.log_this('aaaaaa')
  • Using currentIndexChanged[()].connect(...) to get the version of the signal without arguments - this should work with PyQt5, I don't know about PySide.
  • Adding a positional argument like _idx to log_this before val
The Compiler
  • 11,126
  • 4
  • 40
  • 54