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. I'll tr开发者_开发百科y 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.
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
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
精彩评论