开发者

wxPython: Sending a signal to several widgets

开发者 https://www.devze.com 2022-12-25 07:00 出处:网络
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.

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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号