[Kde-bindings] Python Qt4 Newbie

Albert Cervera i Areny albert at nan-tic.com
Sun Aug 24 22:55:54 UTC 2008


A Diumenge 24 Agost 2008, Ruben Fonseca va escriure:
> On Thu, 2008-08-21 at 10:50 +0100, Ruben Fonseca wrote:
> > > >>> x = lambda : a*4
> > > >>> x()
> > >
> > > Traceback (most recent call last):
> > >   File "<stdin>", line 1, in <module>
> > >   File "<stdin>", line 1, in <lambda>
> > > NameError: global name 'a' is not defined
> > >
> > > >>> a = 3
> > > >>> x()
> > >
> > > 12
> >
> > Isn't it possible to define real anonymous functions in Python, without
> > this lexical/binding thing?
>
> I guess I just found the answer to my problem :)
>
> ------
>
> from functools import partial
>
> for button in [self.button_1, self.button_2, ..., self.button_0]:
>     QtCore.QObject.connect(button, QtCore.SIGNAL('clicked()'),
> partial(self.button_clicked, button))
>
> def button_clicked(self, button):
>     print "yeah!"

For this specific problem you could use the 'QObject.sender()' function, to 
know who sent the signal. No need to specify the object as a parameter.

>
> ------
>
> works like a charm, and seems elegant enough to me :)
>
> Cheers,
> Ruben



-- 
Albert Cervera i Areny
http://www.NaN-tic.com



More information about the Kde-bindings mailing list