4

I have a function defined like this:

def func(self, boolVal):

and I want to create a connection between QPushButton() and this function like this:

self.button1.clicked.connect(partial(self.func, False))

when I run this, it tells me that func() takes exactly 2 arguments (3 given) anyone knows why this could happened?

3
  • 1
    Probably the button passes some event parameter to the function. You could check by allowing for a third parameter and printing it. Commented Oct 7, 2013 at 10:37
  • is there any way to check this ? Commented Oct 7, 2013 at 10:40
  • What happen if you replace the line with self.button.clicked.connect(lambda: self.func(False))? Commented Oct 7, 2013 at 10:50

2 Answers 2

5

functools.partial works fine.

See following example:

from functools import partial
from PyQt4.QtGui import *

class MyWindow(QWidget):
    def __init__(self):
        super(QWidget, self).__init__()
        self.button = QPushButton('test', parent=self)
        self.button.clicked.connect(partial(self.func, False))
        self.button.show()
    def func(self, boolVar):
        print boolVar

app = QApplication([])
win = MyWindow()
win.show()
app.exec_()

If you still get error replace func signature with:

def func(self, boolVar, checked):
    print boolVar
Sign up to request clarification or add additional context in comments.

Comments

0

The first argument is the self parameter, which is bound when you write self.func. The second argument is the False you supplied to partial, so when Qt calls it with a third bool checked parameter from QPushButton.clicked you end up with 3 arguments.

Just write:

self.button1.clicked.connect(self.func)

However, the checked parameter is optional so func should be defined as:

def func(self, checked=False):

1 Comment

but I need to pass a value to func (true or false)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.