[PyQt] lambda slot problem

Linos info at linos.es
Fri Apr 3 08:13:25 BST 2009


Ok, reading about the scoping problem with lambda i now understand the problem, 
activated it is the bool emitted from the clicked signal so i have changed it to:

lambda activated, m=msg: self.changeStatus(m, activated)

and now works correctly, thanks Brian. If i have understand the way that lambda 
works i should define inside the lambda expression any variable that i am using 
in the call and can change in the future, no?

Regards,
Miguel Angel.

Brian Kelley escribió:
> You are seeing a scoping issue, to be safe, I would write the lambda as 
> follows
> 
> lambda changeStatus=self.changeStatus,m=msg,a=activated: changeStatus(m,a)
> 
> This ensures that the bindings of self, activated and msg are what you 
> expect when the lambda function is executed.
> 
> On 4/2/09 7:16 PM, "Linos" <info at linos.es> wrote:
> 
>     Hello,
>             i suppose i am making any mistake here but i dont know why,
>     for example:
> 
>     for checkbox, msg in ((self.printedCheckBox, "printed"),
>     (self.finishedCheckBox,
>     "finished")):
>             self.connect(checkbox, SIGNAL("clicked(bool)"), lambda
>     activated:
>     self.changeStatus(msg, activated))
> 
>     This one ever passes the last msg, "finished", although the checkbox
>     self.printedCheckBox is checked.
> 
>     self.connect(checkbox, SIGNAL("clicked(bool)"),
>     functools.partial(self.changeStatus, msg)   works ok, why the lambda
>     it is only
>     using the last msg string ever?
> 
>     Regards,
>     Miguel Angel.
>     _______________________________________________
>     PyQt mailing list    PyQt at riverbankcomputing.com
>     http://www.riverbankcomputing.com/mailman/listinfo/pyqt
> 



More information about the PyQt mailing list