[英]wxPython: Sending a signal to several widgets
I am not even sure how to ask this question. 我什至不知道如何问这个问题。 I want something that is like the wxPython event system, but a bit different.
我想要的东西类似于wxPython事件系统,但是有点不同。 I'll try to explain.
我会尽力解释。
When there is a certain change in my program (a "tree change", never mind what that is,) I want to send a signal to all the widgets in my program, notifying them that a "tree change" has occurred, and they should change their display in response. 当我的程序中有某个更改时(“树更改”,不用管那是什么),我想向程序中的所有小部件发送信号,通知他们发生了“树更改”,并且它们应相应地更改其显示。
How do I do this? 我该怎么做呢? It sounds a little bit like wxPython events, but not really, since events don't spread to all widgets, as far as I know.
听起来有点像wxPython事件,但实际上并非如此,因为据我所知,事件并未传播到所有小部件。
What would be a good way to do this? 什么是做到这一点的好方法?
You can write your own publish-subscribe mechanism which can be as simple as this: 您可以编写自己的发布-订阅机制,它可以像这样简单:
def register(self, callback):
self.callbacks.append(callback)
def emit(self, eventName):
for callback in self.callbacks:
callback(eventName)
Anybody interested in listening to event registers a function with central registry and then you can emit a event to interested parties, you can improve it further by having to register for a specific event, having multiple registrars, unregister, error checking etc 任何有兴趣听事件的人都可以在中央注册表中注册一个功能,然后您可以将事件发送给感兴趣的各方,您可以通过注册特定事件,拥有多个注册商,注销,错误检查等来进一步改善事件
Alternatively you can use wxPython's wx.lib.pubsub module or other python libraries like PyPubsub , PyDispatcher 或者,您可以使用wxPython的wx.lib.pubsub模块或其他python库,例如PyPubsub , PyDispatcher
check out the observer design pattern. 查看观察者的设计模式。 you need to implement the widgets as an observers .
您需要将小部件实现为观察者。 and the signal sender as the subject.
以信号发送者为主题。 so whenever it the subject sends a signal, all the observers will be notified.
因此,只要对象发送信号,就会通知所有观察者。
check this out for more info about observers 查看此以获取有关观察者的更多信息
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.