I am rewriting portions of the interface of an old Python program written using glade and PyGTK.
the program is quite complex, parts of the functionality are in the plugins, so the original author organized the definition of the interface in several glade files. In addition, a window defines a common interface, and then the specialization comes with its own widgets that fit into a common graphical interface.
I am perplexed how to do this correctly using
from gi.repository import Gtk
builder = Gtk.Builder()
the builder, a global variable, contains all the gtk objects defined in the glade files that I pass with the method add_from_file.
now I want to add several expanders, showing one line of information when collapsing and the image and additional text information when expanding.
these expanders and the main window come from the same file picture_expander.glade, which looks something like this:
<interface>
<object class="GtkWindow" id="picture_preview_window">
<child>
<object class="GtkScrolledWindow" id="scrolledwindow2">
<child>
<object class="GtkViewport" id="viewport1">
<child>
<object class="GtkBox" id="notes_expander_box">
<property name="orientation">vertical</property>
<child>
<placeholder/>
</child>
</object>
</child>
</object>
</child>
</object>
</child>
</object>
<object class="GtkBox" id="notes_box">
<property name="orientation">vertical</property>
<child>
<object class="GtkExpander" id="notes_expander">
<child>
<object class="GtkBox" id="vbox2">
<property name="orientation">vertical</property>
<child>
<object class="GtkTable" id="table1">
<property name="n_columns">2</property>
<child>
<object class="GtkLabel" id="label3">
<property name="label" translatable="yes">User</property>
</object>
<packing>
<property name="x_options">GTK_FILL</property>
<property name="y_options"/>
</packing>
</child>
</object>
<packing>
</packing>
</child>
<child>
<object class="GtkBox" id="picture_frame">
<property name="orientation">horizontal</property>
<child>
<placeholder/>
</child>
</object>
</child>
</object>
</child>
<child type="label">
<object class="GtkLabel" id="label1">
</object>
</child>
</object>
</child>
<child>
<object class="GtkSeparator" id="hseparator2">
<property name="orientation">horizontal</property>
</object>
</child>
</object>
</interface>
What is the right way to do this?
if I add picture_expander.gladeto the constructor, no matter how many times I add it, the builder will create one object, not a lot of it. if I understand correctly, this is exactly why the builder class was introduced. but this way I do not get different objects that I can add to my container notes_expander_box.
I see different options, but I do not know what is the safest and the most offered.
, ?
, ?
, , , notes_expander_box, , , , , , .
# -*- coding: utf-8 -*-
#/usr/bin/env python
from gi.repository import Gtk
builder = Gtk.Builder()
builder.add_from_file("picture_preview.glade")
win = builder.get_object('picture_preview_window')
ne = builder.get_object('notes_expander_box')
for i in range(1, 4):
tb = Gtk.Builder()
tb.add_from_file("picture_preview.glade")
box = tb.get_object('notes_box')
ne.add(box)
win.show_all()
Gtk.main()
, , , , , .
- Jinja2 glade xml , ? ?
, , , .