pygtk gtk.Builder.connect_signals 到多个对象上?
我正在将一些代码从使用 libglade 更新为 GtkBuilder,这应该是未来的方式。
使用gtk.glade,您可以重复调用glade_xml.signal_autoconnect(...)
将信号连接到程序中不同窗口对应的不同类的对象上。然而 Builder.connect_signals 似乎只能工作一次,并且(因此)对传入的第一个类中未定义的任何处理程序发出警告。
我意识到我可以手动连接它们,但这似乎有点费力。 (或者就此而言,我可以使用一些 getattr hackery 让它通过代理将它们连接到所有对象...)
这是一个错误,没有功能可以跨多个对象连接处理程序吗?或者我错过了什么?
其他人也有类似的问题 http://www.gtkforums.com/about1514.html 我假设意味着这是不可能完成的。
I am updating some code from using libglade to GtkBuilder, which is supposed to be the way of the future.
With gtk.glade, you could call glade_xml.signal_autoconnect(...)
repeatedly to connect signals onto objects of different classes corresponding to different windows in the program. However Builder.connect_signals
seems to work only once, and (therefore) to give warnings about any handlers that aren't defined in the first class that's passed in.
I realize I can connect them manually but this seems a bit laborious. (Or for that matter I could use some getattr hackery to let it connect them through a proxy to all the objects...)
Is it a bug there's no function to hook up handlers across multiple objects? Or am I missing something?
Someone else has a similar problem http://www.gtkforums.com/about1514.html which I assume means this can't be done.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我只是一个新手,但这就是我所做的,也许它可以启发;-)
我从“控件”实例化主要组件并传递构建器对象,以便实例化对象可以使用任何构建器对象( mainwindow 示例)或添加到构建器(aboutDialog 示例)。我还传递了一个字典(dic),其中每个组件都向其中添加“信号”。
然后执行“connect_signals(dic)”。
当然,当我需要将用户参数传递给回调方法时,我需要进行一些手动信号连接,但这些很少。
编辑:自动将信号附加到回调的替代方法:
未经测试的代码
I'm only a novice but this is what I do, maybe it can inspire;-)
I instantiate the major components from a 'control' and pass the builder object so that the instantiated object can make use of any of the builder objects (mainwindow in example) or add to the builder (aboutDialog example). I also pass a dictionary (dic) where each component adds "signals" to it.
Then the 'connect_signals(dic)' is executed.
Of course I need to do some manual signal connecting when I need to pass user arguments to the callback method, but those are few.
Edit: alternative to auto attach signals to callbacks:
Untested code
这是我目前所拥有的。请随意使用它,或者提出更好的建议:
Here's what I currently have. Feel free to use it, or to suggest something better:
一段时间以来,我一直在寻找解决方案,发现可以通过将所有处理程序的字典传递给 connect_signals 来完成。
检查模块可以使用提取方法
inspect.getmembers(instance, predicate=inspect.ismethod
然后可以使用
d.update(d3)
将它们连接到字典中,同时注意on_delete
等重复函数。示例代码:
这不会选取使用 @alias 声明的别名方法名称。有关如何执行此操作的示例,请参阅
def dict_from_callback_obj
处的 Builder.py 代码。I have been looking for a solution to this for some time and found that it can be done by passing a dict of all the handlers to
connect_signals
.The inspect module can extract methods using
inspect.getmembers(instance, predicate=inspect.ismethod
These can then be concatenated into a dictionary using
d.update(d3)
, watching out for duplicate functions such ason_delete
.Example code:
This will not pick up alias method names declared using @alias. For an example of how to do that, see the code for Builder.py, at
def dict_from_callback_obj
.