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()
Member Avatar for masterofpuppets

hi,
I haven't actually used wxPython before but here's an example of how you can do this using Tkinter. Sorry if this doesn't help much:

from Tkinter import *

root = Tk(); root.geometry( "500x500+300+100" )
c = Canvas( root, width = 500, height = 500, bg = "white" ); c.pack()

class MainFrame:
    """Class to move an image on a Canvas screen ( Python 2.5 )"""
    def __init__( self, image ):
        self.__image = image
        self.__x, self.__y = 300, 300
        self.__picture = c.create_image( self.__x, self.__y, image = self.__image )
        self.__move = False

        c.bind( "<Button-1>", self.startMovement )
        c.bind( "<ButtonRelease-1>", self.stopMovement )
        c.bind( "<Motion>", self.movement )

    def startMovement( self, event ):
        if self.__x - 20 <= event.x <= self.__x + self.__image.width() - 20 and self.__y - 30 <= event.y <= self.__y + self.__image.height() - 30:
            self.__move = True

    def stopMovement( self, event ):
        self.__move = False

    def movement( self, event ):
        if self.__move:
            c.delete( self.__picture )
            self.__x, self.__y = event.x, event.y
            self.__picture = c.create_image( self.__x, self.__y, image = self.__image )
        if self.__x - 20 <= event.x <= self.__x + self.__image.width() - 20 and self.__y - 30 <= event.y <= self.__y + self.__image.height() - 30:
            c[ "cursor" ] = "hand2"
        else:
            c[ "cursor" ] = ""


if __name__ == "__main__":      
    im = PhotoImage( file = "man.gif" )
    m = MainFrame( im )

    mainloop()

At least I hope this gives you some ideas what you can do :)

Again sorry if it's not helpful enough :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.