Mouse enter/leave events of a frame

270 views
Skip to first unread message

Andy Wu

unread,
Aug 8, 2006, 10:52:33 PM8/8/06
to wxPytho...@lists.wxwidgets.org

Hi,

I'm having a problem with mouse enter/leave events. I am trying to write
a frame that automatically close it self if the mouse has left the
window for a while. However, I can't bind the event to the frame if the
frame has child panels which usually is the case. The panels cover the
frame so mouse enter/leave event of the frame will never trigger.

I tried to make all children of the frame posting the events to the
frame by calling wx.PostEvent in their event handling function, but this
will trigger the events when moving the mouse across different panels of
the frame and they cannot have their own handlers anymore.

Is there a solution for this? I mean, a frame's MouseEnter/MouseLeave
events are always called as long as the mouse is in the area if the
frame no matter what kind of children the frame is holding.

Many thanks

Andy


This e-mail and any files transmitted with it are confidential and intended solely for the use of the individual or entity to whom it is addressed. If you have received this e-mail in error you must not copy, distribute or take any action in reliance on it. Please notify the sender by e-mail or telephone.
We utilise an anti-virus system and therefore any files sent via e-mail will have been checked for known viruses. You are however advised to run your own virus check before opening any attachments received as we will not in any event accept any liability whatsoever once an e-mail and/or any attachment is received. Any views expressed by an individual within this e-mail do not necessarily reflect the views of Systems Union Group plc or any of its subsidiary companies.

Andy Wu

unread,
Aug 9, 2006, 1:15:16 AM8/9/06
to wxPytho...@lists.wxwidgets.org

Thanks Federico.

That means you customized every windows in your frame? I wonder if you
can just have a customized frame and be free to add any kind of widgets
you want. If that is impossible, I guess I'll have to customized
everything in the frame.

-----Original Message-----
From: Federico Ceccatto [mailto:z.dar...@gmail.com]
Sent: 09 August 2006 12:09
To: wxPytho...@lists.wxwidgets.org
Subject: Re: [wxPython-users] Mouse enter/leave events of a frame

I know this is a very rudimentary solution, and I'm sure someone will
chip in with a solid solution involving the event handler.

I had a similar problem a while ago, and since it was for a minor set
of controls with the identical number and type of childrens (just 3
staticbitmaps and a statictext), what I did was to have all 4 four
windows (panel and childrens) to set/unset each one a particular bit
in a bitmask whenever there was an Enter_Window or Leave_Window event.
If the bitmask dropped to zero, then I knew they were not involved
with the position of the mouse.

-F

---------------------------------------------------------------------
To unsubscribe, e-mail: wxPython-user...@lists.wxwidgets.org
For additional commands, e-mail: wxPython-...@lists.wxwidgets.org

Andy Wu

unread,
Aug 9, 2006, 1:40:49 AM8/9/06
to wxPytho...@lists.wxwidgets.org
I'm doing something similar...but still prefer that children widgets do
not know anything about mouse move in/out as they will probably have
their own event handler on these events.

Your idea seems fine...I'm thinking about the same thing minutes ago, I
will go this way most likely.

Thanks for helping!

-----Original Message-----
From: Federico Ceccatto [mailto:z.dar...@gmail.com]
Sent: 09 August 2006 13:28
To: wxPytho...@lists.wxwidgets.org
Subject: Re: [wxPython-users] Mouse enter/leave events of a frame

Unfortunately, yes, I had to to subclass everything. Mind you, they
all were to begin with, as I was drawing a fancy tab with alpha
blending.

It's not a very good solution for larger problems, tough, some ideas:

Does it have to exit the frame inmediatly upon leaving the app's area?
Otherwise you can rig a Timer to periodically call the static method
wx.GetMousePosition() and compare the results against the area covered
by your frame. Get a rect of the frame with GetClientRect(), offset it
by an (x,y) tuple as provided by [frame].ClientToScreen((0,0)), and
then check if the mouse position is inside the rect, (Offset and
Inside in wx.Rect will be very useful).

Andy Wu

unread,
Aug 9, 2006, 4:23:35 AM8/9/06
to wxPytho...@lists.wxwidgets.org
Yes, that does the trick.

My real code will look a little different because I close the frame by fading it out. But thanks a lot for sharing this.

________________________________________
From: Peter Damoc [mailto:pda...@gmail.com]
Sent: 09 August 2006 16:11


To: wxPytho...@lists.wxwidgets.org
Subject: Re: [wxPython-users] Mouse enter/leave events of a frame

On 8/9/06, Andy Wu <And...@systemsunion.com> wrote:
Is there a solution for this? I mean, a frame's MouseEnter/MouseLeave
events are always called as long as the mouse is in the area if the
frame no matter what kind of children the frame is holding.

How about using a timer and checking to see it the mouse is inside the frame or outside the frame?
You will have to make sure that the mouse starts inside the frame or... implement some kind of a trigger mechanism
Like this:

import wx


class CustomFrame(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self, None)
        self.armed = False
        self.t1 = wx.Timer(self)
        self.t1.Start (100)
        self.Bind(wx.EVT_TIMER, self.OnTimer)
        
    def OnTimer(self, evt):
        mouse_pos = wx.GetMousePosition()
        frame_pos = self.GetPosition()
        frame_size = self.GetSize ()
        rect = wx.Rect(frame_pos[0], frame_pos[1], frame_size[0], frame_size[1])
        if rect.Inside(mouse_pos) and not self.armed:
            self.armed = True
            print "Mouse captured. If the mouse leaves the frame it will die!"
        elif not rect.Inside(mouse_pos) and self.armed:
            print "I've warned you! Now the frame died!"
            self.Close()
            
app =  wx.App(0)
frame = CustomFrame()
frame.Show()
app.MainLoop()

Federico Ceccatto

unread,
Aug 9, 2006, 12:08:42 AM8/9/06
to wxPytho...@lists.wxwidgets.org

Federico Ceccatto

unread,
Aug 9, 2006, 1:28:26 AM8/9/06
to wxPytho...@lists.wxwidgets.org

Peter Damoc

unread,
Aug 9, 2006, 4:11:28 AM8/9/06
to wxPytho...@lists.wxwidgets.org
On 8/9/06, Andy Wu <And...@systemsunion.com> wrote:
Is there a solution for this? I mean, a frame's MouseEnter/MouseLeave
events are always called as long as the mouse is in the area if the
frame no matter what kind of children the frame is holding.

Reply all
Reply to author
Forward
0 new messages