[pygtk] self from gtk.builder

Marco Giusti marco.giusti at gmail.com
Mon Dec 6 05:40:34 WST 2010


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


More information about the pygtk mailing list