gtk.Builder,容器子类和绑定子部件

发布于 2024-07-11 18:19:36 字数 662 浏览 11 评论 0原文

我正在尝试在 gtk.Builder 定义文件中使用自定义容器小部件。 就实例化这些小部件而言,它工作得很好:

#!/usr/bin/env python

import sys 
import gtk 

class MyDialog(gtk.Dialog):
    __gtype_name__ = "MyDialog"


if __name__ == "__main__":
    builder = gtk.Builder()
    builder.add_from_file("mydialog.glade")

    dialog = builder.get_object("mydialog-instance")
    dialog.run()

现在的问题是说我在该对话框中有一个 gtk.TreeView 小部件。 我试图弄清楚如何将该小部件绑定到 MyDialog 实例变量。

我能想到的一种便宜的替代方法是在获取对话框小部件后调用附加方法,如下所示:

dialog = builder.get_object("mydialog-instance")
dialog.bind_widgets(builder)

但这似乎相当尴尬。 有没有人已经解决了这个问题或者对如何去做有更好的想法?

谢谢,

I'm trying to use custom container widgets in gtk.Builder definition files. As far as instantiating those widgets, it works great:

#!/usr/bin/env python

import sys 
import gtk 

class MyDialog(gtk.Dialog):
    __gtype_name__ = "MyDialog"


if __name__ == "__main__":
    builder = gtk.Builder()
    builder.add_from_file("mydialog.glade")

    dialog = builder.get_object("mydialog-instance")
    dialog.run()

Now the question is that say I have a gtk.TreeView widget inside that dialog. I'm trying to figure out how to bind that widget to an MyDialog instance variable.

One cheap alternative I can think of is to call additional method after getting the dialog widget like so:

dialog = builder.get_object("mydialog-instance")
dialog.bind_widgets(builder)

But that seems fairly awkward. Has anyone solved this already or has a better idea on how to go about doing it?

Thanks,

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

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

发布评论

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

评论(1

韬韬不绝 2024-07-18 18:19:36

好吧,我想我回答了我自己的问题。

执行上述操作的一种方法是重写 gtk.Buildable 的 parser_finished(),它可以访问创建类实例本身的构建器对象。 该方法在整个 .xml 文件加载后调用,因此我们可能想要获取的所有其他小部件都已存在并已初始化:

class MyDialog(gtk.Dialog, gtk.Buildable):
    __gtype_name__ = "MyDialog"

    def do_parser_finished(self, builder):
        self.treeview = builder.get_object("treeview1")
        # Do any other associated post-initialization

需要注意的一件事是,由于某种原因(至少对我来说,在 pygtk 2.12 中) ),如果我没有显式继承 gtk.Buildable,即使 gtk.Dialog 已经实现了可构建接口,也不会调用重写方法。

Alright, I guess I answered my own question.

One way to do the above is to override gtk.Buildable's parser_finished(), which gives access to the builder object that created the class instance itself. The method is called after entire .xml file has been loaded, so all of the additional widgets we may want to get hold of are already present and intialized:

class MyDialog(gtk.Dialog, gtk.Buildable):
    __gtype_name__ = "MyDialog"

    def do_parser_finished(self, builder):
        self.treeview = builder.get_object("treeview1")
        # Do any other associated post-initialization

One thing to note is that for some reason (at least for me, in pygtk 2.12), if I don't explicitly inherit from gtk.Buildable, the override method doesn't get called, even thought gtk.Dialog already implements the buildable interface.

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