Hi. I'm new to wxPython and am trying to get the mouse coords set as
title to my frame. Could you please hint me how do I do it right?
I got a Frame (300 by 300) and I need to get the coordinates of the
mouse anywhere on the available screen to be accessible within that
frame. Basically, I tried binding "wx.EVT_MOTION" to the Frame first,
but of course I get the coordinates relative to that Frame, and only
when the mouse is over that Frame. I mean, how do I Bind the event to
the whole screen, or how to do it right?
class Frame(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None, size=(300, 300))
self.timer = wx.Timer(self)
self.Bind(wx.EVT_TIMER, self.update_title)
self.timer.Start(40) # in miliseconds
def update_title(self, event):
pos = wx.GetMousePosition()
self.SetTitle("Your mouse is at (%s, %s)" % (pos.x, pos.y))
app = wx.App(redirect=False)
top = Frame()
top.Show()
app.MainLoop()
···
On 28/07/2011 01:47, Andrey Shipilov wrote:
Hi. I'm new to wxPython and am trying to get the mouse coords set as
title to my frame. Could you please hint me how do I do it right?
I got a Frame (300 by 300) and I need to get the coordinates of the
mouse anywhere on the available screen to be accessible within that
frame. Basically, I tried binding "wx.EVT_MOTION" to the Frame first,
but of course I get the coordinates relative to that Frame, and only
when the mouse is over that Frame. I mean, how do I Bind the event to
the whole screen, or how to do it right?
To convert coordinates that are relative to the frame into values relative to the screen you can use frame.ClientToScreen(pos). To continue receiving mouse events even when the cursor is outside the bounds of the frame you can use frame.CaptureMouse(), but be careful because this means that no other window can receive clicks or other mouse events, so you'll need to have some way to trigger a call to ReleaseMouse.
Another approach you could take that would not require capturing the mouse is to use a timer and in the timer's event handler call wx.GetMouseState(). That will return an object with info about the current mouse position and the current state of the buttons. You won't see every event this way, but if all you need is to know where the mouse was located within the last tenth of a second or so then it would work fine.
···
On 7/27/11 5:47 PM, Andrey Shipilov wrote:
Hi. I'm new to wxPython and am trying to get the mouse coords set as
title to my frame. Could you please hint me how do I do it right?
I got a Frame (300 by 300) and I need to get the coordinates of the
mouse anywhere on the available screen to be accessible within that
frame. Basically, I tried binding "wx.EVT_MOTION" to the Frame first,
but of course I get the coordinates relative to that Frame, and only
when the mouse is over that Frame. I mean, how do I Bind the event to
the whole screen, or how to do it right?
Thanks man. That works well.
A small addon. How do I bind the drawing action to mouse movement
event?
I suppose I cannot bind the dc.DrawPoint() to the EVT_TIMER? At least
the traceback says so.
Sorry for noob questions, but I kinda fall off reading the docs, not
getting them much, but I try.
···
On Jul 28, 10:52 pm, Steven Sproat <spro...@gmail.com> wrote:
On 28/07/2011 01:47, Andrey Shipilov wrote:
> Hi. I'm new to wxPython and am trying to get the mouse coords set as
> title to my frame. Could you please hint me how do I do it right?
> I got a Frame (300 by 300) and I need to get the coordinates of the
> mouse anywhere on the available screen to be accessible within that
> frame. Basically, I tried binding "wx.EVT_MOTION" to the Frame first,
> but of course I get the coordinates relative to that Frame, and only
> when the mouse is over that Frame. I mean, how do I Bind the event to
> the whole screen, or how to do it right?
Use a timer?
import wx
class Frame(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None, size=(300, 300))
self.timer = wx.Timer(self)
self.Bind(wx.EVT_TIMER, self.update_title)
self.timer.Start(40) # in miliseconds
def update\_title\(self, event\):
pos = wx\.GetMousePosition\(\)
self\.SetTitle\("Your mouse is at \(%s, %s\)" % \(pos\.x, pos\.y\)\)
app = wx.App(redirect=False)
top = Frame()
top.Show()
app.MainLoop()
Thanks Ryan.
Looks like I take the Timer approach. Just as Steven says.
···
On Jul 29, 12:15 am, Robin Dunn <ro...@alldunn.com> wrote:
On 7/27/11 5:47 PM, Andrey Shipilov wrote:
> Hi. I'm new to wxPython and am trying to get the mouse coords set as
> title to my frame. Could you please hint me how do I do it right?
> I got a Frame (300 by 300) and I need to get the coordinates of the
> mouse anywhere on the available screen to be accessible within that
> frame. Basically, I tried binding "wx.EVT_MOTION" to the Frame first,
> but of course I get the coordinates relative to that Frame, and only
> when the mouse is over that Frame. I mean, how do I Bind the event to
> the whole screen, or how to do it right?
To convert coordinates that are relative to the frame into values
relative to the screen you can use frame.ClientToScreen(pos). To
continue receiving mouse events even when the cursor is outside the
bounds of the frame you can use frame.CaptureMouse(), but be careful
because this means that no other window can receive clicks or other
mouse events, so you'll need to have some way to trigger a call to
ReleaseMouse.
Another approach you could take that would not require capturing the
mouse is to use a timer and in the timer's event handler call
wx.GetMouseState(). That will return an object with info about the
current mouse position and the current state of the buttons. You won't
see every event this way, but if all you need is to know where the mouse
was located within the last tenth of a second or so then it would work fine.
--
Robin Dunn
Software Craftsmanhttp://wxPython.org
Save the mouse info that is needed for your drawing, and then call self.Refresh(). That will result in a paint event an in that handler you can use the saved info to draw the contents of the window. There is some similar things shown in the ScrolledWindow sample in the demo, and in other places too. This sample also shows the approach using CaptureMouse/ReleaseMouse that I mentioned yesterday.
Thanks man. That works well.
A small addon. How do I bind the drawing action to mouse movement
event?
I suppose I cannot bind the dc.DrawPoint() to the EVT_TIMER? At least
the traceback says so.
Sorry for noob questions, but I kinda fall off reading the docs, not
getting them much, but I try.
Thanks for the tip, Robin. And excuse me for calling your Ryan
previously...
I'm trying to understand what happens in the ScrolledWindow.py example
to modify the "app" I did. As the mouse moves I draw a point on the
Panel, and of course as soon as the Refresh() is called the Panel is
cleared and the point is being drawn in the new place.
I'm just trying to understand the wx programming approach cause to me
it seemed like I needed to bind the drawing event to mouse movement
and that would suffice. Looks like not. What I need is to have all the
mouse trails (all dots drawn on the... plot), not sure if I need the
Lines, cause the Timer of 40 ms is really smooth. Here's a screenshot
of what I'm trying to do: Mouse pointer track | Mouse pointer track after 3 hours of w… | Flickr
Anyway, I think I still need some kind of a good manual for wx.
···
On Jul 29, 9:39 pm, Robin Dunn <ro...@alldunn.com> wrote:
On 7/29/11 9:55 AM, Andrey Shipilov wrote:
> Thanks man. That works well.
> A small addon. How do I bind the drawing action to mouse movement
> event?
> I suppose I cannot bind the dc.DrawPoint() to the EVT_TIMER? At least
> the traceback says so.
> Sorry for noob questions, but I kinda fall off reading the docs, not
> getting them much, but I try.
Save the mouse info that is needed for your drawing, and then call
self.Refresh(). That will result in a paint event an in that handler
you can use the saved info to draw the contents of the window. There is
some similar things shown in the ScrolledWindow sample in the demo, and
in other places too. This sample also shows the approach using
CaptureMouse/ReleaseMouse that I mentioned yesterday.
You can draw things on a window at just about any time, but the system does not keep track of that drawing itself, if you want it to remain on the window then you need to keep track of it yourself so you can redraw it when the window becomes partially or fully "damaged," which can happen when another window is moved across your window, it is moved off screen, it is resized, etc. or when Refresh() is called. The system sends paint events when something has happened to damage the content of the window.
Over the past several years there has been a shift from the approach of drawing immediately with wx.ClientDC and then redrawing from the paint event, to simply updating your local buffer and/or other data representing the drawing and then calling Refresh and doing all of the actual drawing in the paint handler. In fact this is the most optimal way to draw window contents on OSX now, because of the way they implement the display subsystem.
···
On 7/31/11 4:29 PM, Andrey Shipilov wrote:
Thanks for the tip, Robin. And excuse me for calling your Ryan
previously...
I'm trying to understand what happens in the ScrolledWindow.py example
to modify the "app" I did. As the mouse moves I draw a point on the
Panel, and of course as soon as the Refresh() is called the Panel is
cleared and the point is being drawn in the new place.