1

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?

2 Answers 2

6

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

Sign up to request clarification or add additional context in comments.

3 Comments

pubsub is the way to go imo :)
@StevenSproat you should say why
Note: PyPubSub listed in the answer is a standalone project but it is also distributed with wxPython as wx.lib.pubsub (which is none other than PyPubSub/src/pubsub from source distribution). PyPubSub has the advantage (over the register/emit example) of supporting hierarchies of messages, documentable message types, exception handling for listeners, and notification of pub-sub activity, features that you will find really important in larger applications.
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.