Reputation: 369
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
Reputation: 59604
Use lambda:
self.myButton.clicked.connect(lambda : self.someVariable = None)
Notes:
self
is referenced upon slot call, so you might want to do:
self.myButton.clicked.connect(lambda self=self: self.someVariable = None)
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