Hey
I'm trying to implement click-and-drag functionality on a frame (or, in this example, an image - but same difference). I need this because I want to run the app with wx.BORDER_NONE, which means no neat titlebar to drag.
The code I'm using at the moment is this: (ignore the image processing code, that's not really relevant)
class gui_main(wx.Frame):
"""
This is the main app window.
"""
def __init__(self, parent, id, title):
wx.Frame.__init__(self, None, id, title, size=(250,250), style=wx.BORDER_NONE)
# image processing. should put this in a def later
img = Image.open('src.jpg')
img = img.resize((250,250), Image.ANTIALIAS)
img.save(os.getcwd() + '\\250px.jpg')
bmp = wx.Bitmap('250px.jpg')
self.bg = wx.StaticBitmap(self, wx.ID_ANY, bmp, (0, 0))
self.bg.Bind(wx.EVT_MOTION, self.evt_motion)
def evt_motion(self, e):
if e.LeftIsDown():
(x, y) = e.GetPositionTuple()
# delta
dx = x - self._mouseX
dy = y - self._mouseY
self.SetPosition((self.GetPositionTuple()[0]+dx, self.GetPositionTuple()[1]+dy))
# always set current mouse pos
(self._mouseX, self._mouseY) = e.GetPositionTuple()
e.Skip()
Now, that works okay. There are two problems with the end result, though.
- The motion is very juddery. Since this is for me, that's kinda acceptable, but I'd rather it didn't do that!
- It isn't moving the window fast enough. If I move the mouse too quickly, it goes outside the window and loses drag functionality.
Any ideas on how to improve the code so that these can be fixed?
Alternatively, is there a better way to do this that I'm missing?