[pygtk] self from gtk.builder

Leon Bogaert leon at tim-online.nl
Mon Dec 6 08:28:12 WST 2010


Thanks Marco,

Messing with __getattr__... I think I'm going to let that pass. It feels kinda messy.
I'm just begining with gtk and I've been browsing through other people's code but I find it difficult to organize my gtk code.

Regards,
Leon

________________________________________
From: pygtk-bounces at daa.com.au [pygtk-bounces at daa.com.au] on behalf of Marco Giusti [marco.giusti at gmail.com]
Sent: Sunday, December 05, 2010 22:40
To: pygtk at daa.com.au
Subject: Re: [pygtk] self from gtk.builder

On Sun, Dec 05, 2010 at 08:54:28PM +0000, Leon Bogaert wrote:
> Hi all,
>
> Is it possible to do something like this?
>
> class ActionsMenu(gtk.Menu):
>     def __init__(self):
>         builder = gtk.Builder()
>         builder.add_from_file("bctimer.xml")
>
>         self = builder.get_object("menuActions")

yes but it doesn't do what you think: it rebinds the name `self` to the
builder object but the other references point to the `ActionsMenu`.

what you can do is something like the following:

        class BaseWindow(object):

                def __init__(self, builder, auto_connect=True):
                        self.__builder = builder
                        if auto_connect:
                                builder.connect_signals(self)

                def __getattr__(self, n):
                        w = self.__builder.get_object(n)
                        if w is not None:
                                return w
                        raise AttributeError, n

or even, but not tested:

                ...
                def __getattr__(self, n):
                        w = self.__builder.get_object(n)
                        if w is not None:
                                setattr(self, n, w) # here the point
                                return w
                        raise AttributeError, n

m.


--
Excellentium virorum est improborum negligere contumeliam,
a quibus etiam laudari turpe.
                -- Plutarco
_______________________________________________
pygtk mailing list   pygtk at daa.com.au
http://www.daa.com.au/mailman/listinfo/pygtk
Read the PyGTK FAQ: http://faq.pygtk.org/


More information about the pygtk mailing list