Sebastian Elsner
Sebastian Elsner

Reputation: 369

PyQt signal: set variable

I have a signal, which just needs to do one thing: if emitted assign a variable a given value. It seems stupid to create a method for this, so I was thinking: Is there a way to do something like this:

self.myButton.clicked.connect(self.someVariable = None)

Upvotes: 2

Views: 3411

Answers (1)

warvariuc
warvariuc

Reputation: 59604

Use lambda:

self.myButton.clicked.connect(lambda : self.someVariable = None)

Notes:

  1. self is referenced upon slot call, so you might want to do:

    self.myButton.clicked.connect(lambda self=self: self.someVariable = None)

  2. Some widget send signals with parameters, and you have to put them in lambda definition.

        self.myButton.clicked.connect(lambda checked=False, self=self: self.someVariable = None)

UPDATE:

Assignments are not allowed in lambda, so you will have to use real function or something like this:

self.myButton.clicked.connect(lambda self=self: setattr(self, 'someVariable', None))

Upvotes: 2

Related Questions