gtk.Builder,容器子类和绑定子部件
我正在尝试在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好吧,我想我回答了我自己的问题。
执行上述操作的一种方法是重写 gtk.Buildable 的 parser_finished(),它可以访问创建类实例本身的构建器对象。 该方法在整个 .xml 文件加载后调用,因此我们可能想要获取的所有其他小部件都已存在并已初始化:
需要注意的一件事是,由于某种原因(至少对我来说,在 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:
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.