Order of events

Robin Dunn:

Oswaldo Hernández wrote:

The problem is:

When the same event is binded in baseTextCtrl and S01_TextCtrl the events are raised in the following order:

First on S01_TextCtrl
Second on baseTextCtrl

I Need that the event on baseTextCtrl be raised before than S01_TextCtrl.

Is this possible?

There are two ways to do it. First is to not bind the base class handlers until after the derived class has bound its handlers. The second is to derive a new class from wx.EvtHandler, bind your base class handlers to an instance of this class instead of self, and then push that instance on the head of the evt handler chain with self.PushEventHandler(instnace).

A third way would be to use the same name for the event handling methods in base and subclass and then explicitly call the base class event handler before or after dealing with the event in the subclass. For example:

baseTextCtrl(wx.TextCtrl):
     def __init__(self, ...):
         ...
         self.Bind(wx.EVT_FOCUS, self.onSetFocus)

     def onSetFocus(self, event):
         # whatever

S01_TextCtrl(baseTextCtrl):
     def __init__(self, *args, **kwargs)
         super(S01_TextCtrl, self).__init__(*args, **kwargs)
         # No need to bind to wx.EVT_FOCUS again

     def onSetFocus(self, event):
         # onSetFocus extends baseTextCtrl.onSetFocus
         # You can do something before baseTextCtrl deals with the event
         # here
         super(S01_TextCtrl, self).onSetFocus(event)
         # And/or you can do something after baseTextCtrl has dealt
         # with the event here

I would even argue that this is the best way because S01_TextCtrl is extending the behavior of baseTextCtrl. It looks kind of weird to me that a (text)control would get a separate focus event for each of the (super)classes of the instance... Or in other words, in your solution the control will get focus once conceptually, but will receive multiple wx.EVT_FOCUS events, all delivered to different methods of the same object.

Cheers, Frank