views:

124

answers:

2

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 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.

What would be a good way to do this?

+5  A: 

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)

So basically 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, error checking etc

Alternatively you can use already available wx.lib.pubsub module in wxPython.

And there are more python based libraries e.g. PyPubsub , PyDispatcher

Anurag Uniyal
pubsub is the way to go imo :)
Steven Sproat
A: 

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

Moayyad Yaghi