如何使用 GtkBuilder 重用空地文件中的小部件树?

发布于 2024-10-06 07:06:54 字数 421 浏览 1 评论 0原文

我想即时填充 gtk.notebook。每次用户打开文件时,都会生成一个新的笔记本选项卡。非常简单。我的问题是,我使用空地构建用户界面,笔记本选项卡应该有一个子小部件树(滚动窗口->视口->对齐->框架)。在我的空地文件中,我有一个模板笔记本选项卡,我想多次使用它,这样我就不必用普通的 gtk 来编码整个树。使用 libglade,您可以重用小部件树,如 pygtk 常见问题解答中所述: http://faq.pygtk.org/index.py?file=faq22.011.htp&req=show 。我如何使用 GtkBuilder 做到这一点?

预先感谢,

亚瑟

i want to populate a gtk.notebook on-the-fly. everytime a user opens a file, a new notebook-tab is generated. pretty straight forward. my problem is, that i use glade to build the ui and the notebook-tab should have a child widget tree (scrolledwindow->viewport-> alignment->frame). in my glade-file, i have a template notebook-tab, which i want to use multiple times, so that i dont have to code the whole tree in plain gtk. with libglade, you could reuse a widget tree as explained in the pygtk faq here: http://faq.pygtk.org/index.py?file=faq22.011.htp&req=show . How do i do this with GtkBuilder?

thanks in advance,

Arthur

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

無心 2024-10-13 07:06:54

使用 GtkBuilder 这样做:

builder = gtk.Builder()
builder.add_from_file("GUI.xml")
builder.connect_signals(self)
self.window1 = builder.get_object("window1")
self.window1.show()

编辑:

我最初是错的,似乎 gtkbuilder 在添加时确实实例化了对象。因此,理想的方法是通过字符串手动添加小部件

builder.add_from_string("""
<interface>
  <object class="GtkWindow" id="window1">
    <child>
      <object class="GtkComboBox" id="combobox1">
        <property name="model">liststore1</property>
      </object>
    </child>
  </object>
</interface>""")

self.window1 = builder.get_object("window1")

希望这可行!

Do it this way with GtkBuilder:

builder = gtk.Builder()
builder.add_from_file("GUI.xml")
builder.connect_signals(self)
self.window1 = builder.get_object("window1")
self.window1.show()

edit:

I was initially wrong, it seems that gtkbuilder does instantiate objects when it adds. So the ideal way to do this would be to add the widget in manually via a string

builder.add_from_string("""
<interface>
  <object class="GtkWindow" id="window1">
    <child>
      <object class="GtkComboBox" id="combobox1">
        <property name="model">liststore1</property>
      </object>
    </child>
  </object>
</interface>""")

self.window1 = builder.get_object("window1")

Hopefully this works!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文