Investigating mouse events

Uhm, I’m afraid you’re way off your mark here. Sometimes you can dig deeper by simply firing up a shell and

>>> import wx

Now, first of all, names like “wx.EVT_***” are not event types at all, they are event binders, and, in fact, objects (yes, despite their constant-like appearance… it’s a long story…):

>>> wx.EVT_BUTTON # this guy is really an object in disguise... 
<wx.core.PyEventBinder object at 0x.....>

Event types are attached to binders and they are, indeed, numerical constants. They are stored in the “evtType” attribute of the binder object:

>>> wx.EVT_BUTTON.evtType
[10012]

except, the “evtType” attribute is really a list, not a number yet. This is because, while most binders have just one event type, a few “collective” binders may have many types attached:

>>> wx.EVT_MOUSE_EVENTS.evtType
[10030, 10031, 10032, 10033, 10034, 10035, 10036, 10039, 10040, 10041, 
 10037, 10038, 10045, 10046, 10047, 10048, 10049, 10050, 10051]

Now for the fun part: every single event type (numerical constant) also comes with its own symbol, in the form of “wx.wxEVT_***”:

>>> wx.wxEVT_BUTTON  # now, that's what I call a *number*!
10012

Of course you cannot have also a “wx.wxEVT_MOUSE_EVENTS”, but you do have the likes of

>>> wx.wxEVT_LEFT_UP
10031
>>> wx.wxEVT_LEFT_DOWN
10030

and so on.

Wrapping things up, your code works fine either if you write

    def OnMouseEvent (self, evt):
        et = evt.GetEventType()  # this is a *number*
        if [et] == wx.EVT_ENTER_WINDOW.evtType:  # but evtType is a *list* !
            print ("EVT_ENTER_WINDOW")
        elif [et] == wx.EVT_LEAVE_WINDOW.evtType:
            print ("EVT_LEAVE_WINDOW")
        # etc etc

or

    def OnMouseEvent (self, evt):
        et = evt.GetEventType()  # this is a *number*
        if et == wx.wxEVT_ENTER_WINDOW:  # this is a number too!
            print ("EVT_ENTER_WINDOW")
        elif et == wx.wxEVT_LEAVE_WINDOW:
            print ("EVT_LEAVE_WINDOW")
        # etc etc

With all that being said, no one would ever do something like that.
Just use the handy wx.MouseEvent methods to query what kind of action was performed:

    def OnMouseEvent (self, evt):
        if evt.LeftDown():
            print('left down')
        elif evt.LeftUp():
            print('left up')
        # etc. etc.

or, if you want to save a few lines, even something like

    def OnMouseEvent (self, evt):
        for meth_name in ('LeftDown', 'LeftUp', 'Entering', 'Leaving'): # add to taste
            if getattr(evt, meth_name)():
                print(meth_name)
2 Likes