How to get an event when the mouse is inside a window ?

34 views
Skip to first unread message

ErwinP

unread,
Nov 13, 2012, 2:47:06 PM11/13/12
to wxpytho...@googlegroups.com
Hi,

I want to make a window that gets an event when the mouse enters/leaves the window. My first approach was to catch EVT_ENTER_WINDOW and EVT_LEAVE_WINDOW. But this does not work when my window has child windows, because when the mouse enters the child window, the main window gets a EVT_LEAVE_WINDOW. In the extreme case when the child window fills the entire space of the main window, the main window gets none of the events.
Example:

import wx 

class PanelWithChild(wx.Panel):
    def __init__(self, *a, **kw): 
        wx.Panel.__init__(self, *a, **kw) 
        sizer = wx.BoxSizer(wx.VERTICAL) 
        self.SetSizer(sizer) 
        if 1:
            child = wx.Panel(self, -1)
            child.SetBackgroundColour(wx.RED)
            sizer.Add(child, 1, wx.EXPAND)

class TestFrame(wx.Frame): 
    def __init__(self): 
        wx.Frame.__init__(self, None, -1, "Test") 
        panel = PanelWithChild(self, -1) 
        panel.SetBackgroundColour(wx.BLUE)
        panel.Bind(wx.EVT_ENTER_WINDOW, self.OnEnterWindow, panel)
        panel.Bind(wx.EVT_LEAVE_WINDOW, self.OnLeaveWindow, panel)
        
    def OnEnterWindow(self, event):     
        print "OnEnterWindow", event
        event.Skip()
        
    def OnLeaveWindow(self, event):     
        print "OnLeaveWindow", event
        event.Skip()

app = wx.PySimpleApp() 
TestFrame().Show() 
app.MainLoop()

Without the child in PanelWithChild, I get the events, but with the child I get none of them.
Is there a way to get events when the mouse enters/leaves the target window (PanelWithChild in this example) ?
Modifying the code of the target window is bad because the target window could reside in an external library that I do not want to touch.

Erwin

Robin Dunn

unread,
Nov 16, 2012, 12:53:45 AM11/16/12
to wxpytho...@googlegroups.com
On 11/13/12 11:47 AM, ErwinP wrote:
> Without the child in PanelWithChild, I get the events, but with the
> child I get none of them.
> Is there a way to get events when the mouse enters/leaves the target
> window (PanelWithChild in this example) ?
> Modifying the code of the target window is bad because the target window
> could reside in an external library that I do not want to touch.

One way would be to poll the mouse position yourself in a timer event
using wx.GetMouseState(), and compare its position to the screen rect of
the panel, and when it transitions to/from outside/inside then you can
do what you would have done in the ENTER or LEAVE events.

--
Robin Dunn
Software Craftsman
http://wxPython.org

Reply all
Reply to author
Forward
0 new messages