i want to write a simple app which does nothing else except that you can drag an image in a window(simply clicking somewhere on the screen and moving mouse while holding the left button down).
The problem is that my image moves only once and only a little bit and then stops moving. Could someone help with this problem of mine?
import wx
import cPickle
import os
#------------------------------------------------------------------------------
class MainFrame(wx.Frame):
#------------------------------------------------------------------------------
def __init__(self):
wx.Frame.__init__(self, None, -1, 'Application', size=(1000, 700))
self.Centre()
self.window = wx.Window(self, -1, (457, 0), (900, 700))
self.Shape = wx.Bitmap('OW3.bmp')
self.Shape.pos = (700 ,5)
self.Shape.shown = True
self.Image = None
self.dragShape = None
self.dragImage = None
self.window.Bind(wx.EVT_PAINT, self.Paint)
self.window.Bind(wx.EVT_LEFT_DOWN, self.OnLeftDown)
self.window.Bind(wx.EVT_MOTION, self.OnMotion)
def Paint(self, evt):
if (self.Shape.shown == True):
self.Image = wx.PaintDC(self.window)
self.Image.DrawBitmap(self.Shape, 700, 5, True)
def GetRect(self, dragShape):
return wx.Rect(self.dragShape.pos[0], self.dragShape.pos[1], self.dragShape.GetWidth(), self.dragShape.GetHeight())
def OnLeftDown(self, evt):
self.dragShape = self.Shape
self.dragStartPos = evt.GetPosition()
def OnMotion(self, evt):
if not self.dragShape or not evt.Dragging() or not evt.LeftIsDown():
return
if self.dragShape:
pt = evt.GetPosition()
dx = abs(pt.x - self.dragStartPos.x)
dy = abs(pt.y - self.dragStartPos.y)
self.dragShape.shown = False
self.RefreshRect(self.GetRect(self.dragShape), True)
self.window.Update()
self.dragImage = wx.DragImage(self.dragShape, wx.StockCursor(wx.CURSOR_HAND))
hotspot = self.dragStartPos - self.dragShape.pos
self.dragImage.BeginDrag(hotspot, self.window, True)
self.dragImage.Move(pt)
self.dragImage.Show()
#------------------------------------------------------------------------------
if __name__ == '__main__':
#------------------------------------------------------------------------------
app = wx.PySimpleApp()
MainFrame().Show()
app.MainLoop()