Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

arguments for partial function in python

Tags:

python

pyqt

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?

like image 486
user2234234 Avatar asked Oct 21 '25 19:10

user2234234


1 Answers

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
like image 159
falsetru Avatar answered Oct 24 '25 10:10

falsetru