FW: question Python custome events

Ian Kelly ian.g.kelly at gmail.com
Tue Oct 9 12:45:31 EDT 2012


On Tue, Oct 9, 2012 at 2:35 AM, Hussain, Mushabbar
<Mushabbar.Hussain at honeywell.com> wrote:
>
>
> Hi,
>
> Is it possible to define an Event which should fire when a value of a
> variable changes? Something like below
>
>
>
> self.Bind(wx.EVT_ON_VAL_CHANGE, variable_to_watch, self.Callback)
>
>
>
> I need a Text ctrl UI which continuously changes values based on external
> data changes. Unfortunately I could not find events in Python which can be
> triggered when a variable value changes?  Please let me know how this can be
> achieved?

Yes, you would need to:

1) encapsulate the variable;

2) define a custom event;

3) set up a wx.EvtHandler for the variable, since it's not itself
associated with a window;

4) bind your callback to the event handler;

5) have the variable setter fire the custom event.

That said, an event is likely not going to be the best way to do this.
 You should instead use the wx.lib.pubsub module, which implements the
observer pattern for you.  This will be cleaner and easier, as you
only need to:

1) encapsulate the variable;

2) subscribe your callback to the update message;

3) have the variable setter publish the update message.

Observers are also conceptually simpler than events.  You can find
information about pubsub at:

http://wiki.wxpython.org/WxLibPubSub

Cheers,
Ian



More information about the Python-list mailing list