Tkinter drag and drop Tkinter drag and drop tkinter tkinter

Tkinter drag and drop


This answer to the question "board drawing code to move an oval" shows how to drag an object on a canvas.

In your case, you're not resetting the base of the delta as you move the object. If the mouse moves one pixel to the right, you use move to move the mouse one pixel to the right.

Now, let's say you move it one more pixel to the right. This time, your calculation says the delta is 2 from the starting point even though you only actually moved the mouse one more pixel). Next time you move one pixel, you're calculating a delta of 3, and so on.

The solution is simple: reset dragInfo["xCoord"] and dragInfo["yCoord"] while it is moving, since you only want to compute the delta to its previous position, not the original starting position.

def onPressToMove(self, event): #get initial location of object to be moved    winX = event.x - self.canvas.canvasx(0)    winY = event.y - self.canvas.canvasy(0)    self.dragInfo["Widget"] = self.canvas.find_closest(event.x, event.y, halo = 5)[0]    # reset the starting point for the next move    self.dragInfo["xCoord"] = winX    self.dragInfo["yCoord"] = winY