假设你有这个 ui 文件
<?xml version="1.0" encoding="UTF-8"?>
<!-- Generated with glade 3.15.3 on Tue Sep 17 10:11:35 2013 -->
<interface>
<!-- interface-requires gtk+ 3.10 -->
<object class="GtkGrid" id="grid2">
<property name="visible">True</property>
<property name="row_spacing">12</property>
<property name="row_homogeneous">True</property>
<child>
<object class="GtkLabel" id="label1">
<property name="visible">True</property>
<property name="label" translatable="yes">I'm a widget from the new content</property>
</object>
<packing>
<property name="left_attach">0</property>
<property name="top_attach">0</property>
<property name="width">1</property>
<property name="height">1</property>
</packing>
</child>
<child>
<object class="GtkLabel" id="label2">
<property name="visible">True</property>
<property name="label" translatable="yes">Me too</property>
</object>
<packing>
<property name="left_attach">0</property>
<property name="top_attach">1</property>
<property name="width">1</property>
<property name="height">1</property>
</packing>
</child>
</object>
<object class="GtkWindow" id="window1">
<child>
<object class="GtkGrid" id="grid1">
<property name="visible">True</property>
<child>
<object class="GtkButton" id="button1">
<property name="label" translatable="yes">button</property>
<property name="visible">True</property>
</object>
<packing>
<property name="left_attach">0</property>
<property name="top_attach">0</property>
<property name="width">1</property>
<property name="height">1</property>
</packing>
</child>
<child>
<object class="GtkButton" id="button2">
<property name="label" translatable="yes">button</property>
<property name="visible">True</property>
</object>
<packing>
<property name="left_attach">1</property>
<property name="top_attach">1</property>
<property name="width">1</property>
<property name="height">1</property>
</packing>
</child>
<child>
<object class="GtkButton" id="button3">
<property name="label" translatable="yes">button</property>
<property name="visible">True</property>
</object>
<packing>
<property name="left_attach">2</property>
<property name="top_attach">2</property>
<property name="width">1</property>
<property name="height">1</property>
</packing>
</child>
</object>
</child>
</object>
</interface>
现在,您像往常一样创建您的窗口:
window1 = self.builder.get_object('window2')
window1.show_all()
如果要更改窗口的内容。然后你需要销毁/隐藏你想要隐藏的小部件。
grid1 = self.builder.get_object('grid1')
grid1.destroy()
grid2 = self.builder.get_object('grid2')
window1.add (grid2)
我们都很好。您可以从相同的初始 ui 文件或新文件加载第二个小部件层次结构(我的意思是grid2 )。您需要了解的是,在Builder从 ui 文件创建对象之后,它不能进一步修改这些对象。