[pygtk] Resuing tabs?

Doug Quale quale1 at charter.net
Sat Jul 24 13:47:47 WST 2004


On Fri, Jul 23, 2004 at 10:13:57AM +1200, John C Barstow wrote:
> I have a project where, having done GLADE layout for a notebook, I need
> multiple instances of a certain tab. They will all be children of the
> same notebook.
> I don't really want to layout the tab multiple times, as it's rather
> complex. Is there a simple way to clone tabs that I'm overlooking?

If the number and format of the notebook pages are fixed you can set
up everything in glade.  Copy and paste work pretty well in glade and
can make quick work of this.  Select the widget you want to copy and
use Ctrl-C to copy and Ctrl-V to paste.  Copying a container widget
like a table or a vbox will copy all its children.

Of course once you've done this you may have to fix up a bunch of
widget names and possibly signal names.  This is fine until you decide
you want to make the same change to all n of the pages and then you
have to do it n times.  You could instead try the following method....

If you need to dynamically add an arbitrary number of notebook pages
the path of least resistance is often to do the layout in Python.  If
the pages are complex as in your case, you can still do the layout in
glade if you're careful.

There are several minor variations on this technique so I'll describe
a simple one.  If you make sure that the notebook page widget that you
want to clone has a unique name not duplicated in your glade file, you
can use this function to create a new instance of a widget to add to a
notebook page:

    def clone(glade_file, widget_name):
        """Returns a fresh instance of widget_name from glade_file."""
        glade = gtk.glade.XML(glade_file, widget_name)
        return glade.get_widget(widget_name)

You have to take some care in setting up the controller for the cloned
page.  There are at least two problems.  1) The widgets on the cloned
pages will have duplicate names, although this could be an advantage
if you can also clone the controllers.  Duplicate names are OK if
you're careful with it, although perhaps it could have some adverse
effect on accessibility.  2) The widget children of the clone are hard
to access given only their parent container.  One possibility would be
to return the gtk.glade.XML object instead of the widget itself and
make the caller extract the widgets using get_widget() calls.

I hope this helps.  If I am misinterpreting the problem, feel free to
clarify.


More information about the pygtk mailing list