[pygtk] Problem with simple custom widget

Jeffrey Barish jeff_barish at earthlink.net
Sat Feb 16 03:28:26 WST 2008


On Friday 15 February 2008 02:44:26 Johan Dahlin wrote:
> Jeffrey Barish wrote:
> > As an exercise in preparation for bigger things, I am trying to create a
> > custom widget that is a gtk.Entry with an added label.  I borrowed
> > liberally from examples, but I am not aware of any examples that do quite
> > what I am trying to do.  I can make this widget work by subclassing
> > gtk.Bin and adding the entry as a child, but what I really want is to
> > have a "self-contained" widget.  I think that I am close, but I have two
> > problems (that I know about) that have me completely stumped.  The first
> > is that I can't get the label to appear, although there appears to be
> > space for it.  The second is that the entry does not resize when I resize
> > the window.  Any suggestions would be greatly appreciated.  I'm hoping
> > that creating custom widgets gets easier after the first one.
>
> I am not sure if it's actually possible to do what you want to do.
> But, you could try to set the parent of the widget to the same parent the
> entry has. It may or might not help you.
> The parent is responsible for propagating the drawing requests to all
> children.

Your comments are very interesting.  I was beginning to wonder whether it is 
possible to do what I am trying to do.  I thought that it might be possible 
because it worked when I subclassed gtk.Bin and added the label from outside 
the class as a child.  I figured that I could make it work as a subclass of 
Entry by treating the internal Label as the child.  However, assigning the 
label self as its parent (as I had done with the Bin) did not work because 
self (the entry) is not a container.  Consequently, I did not assign it any 
parent (as you could see), but I now see in the documentation for realize 
that a widget must be inside a top-level window when it is realized.  I 
didn't notice this requirement sooner because the label was not being 
realized (see below).  However, the label still does not appear when I assign 
the parent of the entry to the parent of the label.

I tried connecting the realize signals for self and self.label.  I discovered 
that the label was not being realized.  I tried calling realize on the label 
when I got to self's realize handler.  Now I get to label's handler for 
realize.  Still nothing.  I tried adding a call to its do_realize.  I seem to 
need this call.  As long as it's there, I get a complaint from GTK when I do 
not assign a parent to the label (G_IS_OBJECT (object) failed).  I still see 
nothing.  Perhaps I should give up.

If I do give up, I wonder what is the right approach.  Should I be subclassing 
a container?  Can I subclass a Bin, have the subclass put an HBox in itself, 
and put the Label and Entry in the HBox?  Or perhaps I should subclass HBox 
and have the subclass put the Label and Entry in itself.  Or maybe I should 
just make a factory function that produces HBoxes with the right stuff 
inside.  The last, no doubt, is easiest, but I was hoping to encapsulate 
interactions between the various widgets I will be dealing with.
-- 
Jeffrey Barish


More information about the pygtk mailing list