I am trying to create a simple program where you can move multiple objects on canvas in tkinter by dragging them. For beginning, I started with 2 squares. However, I am facing 2 problems:
anytime I click on object it gets centred to mouse pointer (pointer always in the middle of square). How can I drag the object by place of click (for example drag by corner)?
once I pass with one square through another the 2nd square stays on top and can not be separated any more. The objects have same sizes.
Are there any simple solutions for this? Thank you for any help or advice.
My code:
import tkinter
c = tkinter.Canvas(width = 400, height = 300)
c.pack()
d = 25 #size of square
x, x2 = 100, 200
y, y2 = 100, 200
rect = c.create_rectangle(x-d, y-d, x+d, y+d, fill = 'blue') #first square
rect2 = c.create_rectangle(x2-d, y2-d, x2+d, y2+d, fill = 'red') #second square
def drag(event): #drag by holding mouse button 1
global x, y, x2, y2, xt, yt, x2t, y2t
xt, yt = event.x, event.y #1st square movement coords
x2t, y2t = event.x, event.y #2nd square movement coords
if xt in range(x-d, x+d):
if yt in range(y-d, y+d):
c.coords(rect, xt-d, yt-d, xt+d, yt+d) #coords update of 1st square
x, y = xt, yt
if x2t in range(x2-d, x2+d):
if y2t in range(y2-d, y2+d):
c.coords(rect2, x2t-d, y2t-d, x2t+d, y2t+d) #coords update of 2nd square
x2, y2 = x2t, y2t
c.bind('<B1-Motion>', drag)
tkinter.mainloop()