|FAQ Index - Search - Recent Changes - Everything - Add entry|
22.11. How do I produce or reuse a [portion of a] widget tree in glade?
You can create as many widget trees as you like based on the same glade XML content -- and libglade even caches xml parse trees automatically. You can also grab portions of widget trees by supplying a second parameter to your XML() call specifying the name of the toplevel widget you want.
The example below creates 10 identical dialogs:
for i in range(10): xml = gtk.glade.XML("foo.glade", "my-window") win = xml.get_widget("my-window") win.show()You can also get portions of windows and place them inside other widgets in runtime.
xml = gtk.glade.XML("foo.glade", "my-vbox") vbox = xml.get_widget("my-vbox") win = gtk.Window() win.add(vbox) win.show()You can experiment using reparent(), add() and remove() to compose your interface dynamically, reusing both code and glade XML. The only thing to remember is to make the windows not visible by default, as per FAQ 22.6
NB: The only thing to watch out for is keyboard accelerators, which need to be attached to the main window. This, at least in PyGTK 0.6, was a non-trivial task.