Hi there,
Have been creating an application on Mac OS X using wxPython recently and
came across a slight oddity where the cursor click events on a panel seemed
to be offset by a few pixels to the actual generated drawing. Thus it would
seem like a click was generated outside of a window/panel.
E.g.
X clicked here
----------------------- : Panel
···
o drew point here |
> >
> >
-----------------------
To illustrate this I created a test (see below) where I began to wonder if
the mouse event position and the paint position were in fact the same and it
was the cursor icon itself that was offset. It seems that with my setup the
cursor hotspot for the mouse down event is out by 1x, 2y pixels.
Is there a way to globally change the hotspot offset or fix this some how if
indeed I am on the right path here?
Ta,
Martin Pengelly-Phillips
Example App
-----------------
import wx
# MyPanel
#----------------------------------------------
class MyPanel(wx.Panel):
def __init__(self, *args, **kwargs):
wx.Panel.__init__(self, *args, **kwargs)
self.points = []
self.SetMinSize(wx.Size(200, 200))
self.Bind(wx.EVT_PAINT, self.onPaint)
self.Bind(wx.EVT_LEFT_DOWN, self.onLeftDown)
def onPaint(self, event):
w, h = self.GetClientSize()
dc = wx.BufferedPaintDC(self)
dc.SetBackground(wx.Brush(wx.WHITE))
dc.Clear()
for point in self.points:
dc.DrawPoint(point.x, point.y)
dc.DrawText("%d,%d" % (point.x, point.y), point.x, point.y)
def onLeftDown(self, event):
self.points.append(event.GetPosition())
self.Refresh()
# MyFrame
#----------------------------------------------
class MyFrame(wx.Frame):
def __init__(self, *args, **kwargs):
wx.Frame.__init__(self, *args, **kwargs)
self.box = wx.BoxSizer(wx.VERTICAL)
self.panel = MyPanel(self)
self.clear = wx.Button(self, label="Clear")
self.box.Add(self.panel, 1, wx.EXPAND|wx.ALL, border=15)
self.box.Add(self.clear, 0, wx.EXPAND|wx.ALL, border=5)
self.clear.Bind(wx.EVT_BUTTON, self.onClear)
self.SetSizerAndFit(self.box)
def onClear(self, event):
self.panel.points = []
self.panel.Refresh()
# main entry
#----------------------------------------------
if __name__ == '__main__':
app = wx.App()
frame = MyFrame(None, title="Mismatched")
frame.Centre()
frame.Show()
app.MainLoop()