[pygtk] Re: Re: Cannot subclass gtk.Widget

Gustavo J. A. M. Carneiro gjc at inescporto.pt
Sun Mar 11 03:31:12 WST 2007


On Sáb, 2007-03-10 at 09:54 -0700, Jeffrey Barish wrote:
> Gustavo J. A. M. Carneiro wrote:
> 
> > Another, more complex, example can be found here:
> > http://telecom.inescporto.pt/~gjc/higcontainer/higcontainer.py
> 
> By studying the examples, I have learned a lot about subclassing.  I have
> two questions about this example.  It seems desirable to me that it be
> possible to call the subclass in the same manner used to call other PyGTK
> classes.  Thus, in the case of your example, the subclass would be
> instantiated with
> 
> group = HIGContainer(title='Hello')
> 
> instead of
> 
> group = gobject.new(HIGContainer, title="Hello")
> 
> (In fact, I think it would be even more consistent to have
> 
> group = HIGContainer()
> group.set_title("Hello")

  group.set_title("Hello") is equivalent to group.props.title = "Hello",
and we all know there should be preferably only one way to do it ;-)

> )
> 
> When I make this change, I get the error message
> 
> 'HIGContainer' object has no attribute '_HIGContainer__title'

  The error was caused by gobject setting the default property value.

> 
> The error vanishes when I drop the gobject.PARAM_CONSTRUCT, so evidently
> gobject.new does something with __gproperties__ that does not happen
> otherwise.  What is the implication of dropping the gobject.PARAM_CONSTRUCT
> flag and invoking the HIGContainer this way?

  It was stupid of me to make that property gobject.PARAM_CONSTRUCT; it
means that the property can only be set during object construction,
through gobject.new, and also that if the property isn't explicitly set
by the programmer then it is automatically set to the registered default
value.   But there's no good reason in this case to require that.  I've
updated the code.

>   
> 
> Also, I read in gobject-tutorial that it is "absolutely necessary" to use
> gobject.type_register, yet you didn't.  The program seems to work fine
> whether or not gobject.type_register is present.  What are the
> ramifications of omitting this call?

  In some cases gobject.type_register is called automatically by PyGTK.
This is done through the GObjectMeta metaclass.  The logic it uses to
decide whether or not to call type_register automatically is easy to
follow in the source code:

http://svn.gnome.org/viewcvs/pygobject/trunk/gobject/__init__.py?view=markup


-- 
Gustavo J. A. M. Carneiro
<gjc at inescporto.pt> <gustavo at users.sourceforge.net>
The universe is always one step beyond logic



More information about the pygtk mailing list