Search code examples
wxpythonwxwidgets

wxPython: wx.EVT_LEAVE_WINDOW is called when entering a child control


I'm trying to bind events to increase the opacity only while the mouse is present over a window, however the opacity is reduced whenever the mouse hovers over a child control

    self.Bind(wx.EVT_ENTER_WINDOW, self.SetOpaque)
    self.Bind(wx.EVT_LEAVE_WINDOW, self.SetSemiTransparent)

Is there an alternative to wx.EVT_LEAVE_WINDOW which is does not trigger when hovering over a child?

Example:

Hovering over the button or text box causes the opacity to drop (as defined in SetSemiTransparent):

enter image description here


Solution

  • It's the OS which is triggering those events, not wxWidgets.

    In the parent's handler for wx.EVT_LEAVE_WINDOW find if the current mouse coordinates are inside a child or out of the parent.

    To test if a point is inside a window you can use wx.HitTest.

    Example of the wx.HitTest

    class SomeFrame(wx.Frame):
        def __init__(self):
            super().__init__(None, style=wx.NO_BORDER)
            self.Bind(wx.EVT_LEAVE_WINDOW, self.SomeEventHandler)
    
        def SomeEventHandler(self, event):
            if self.HitTest(event.Position) == wx.HT_WINDOW_OUTSIDE:
                #Process some event.
                pass
    

    See also: