Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Tk_drag_and_drop.py
- from Tkinter import *
- def Click(event):
- X = event.x
- Y = event.y
- [xmin,ymin,xmax,ymax] = canvas.coords(target)
- cv.onTarget = False
- if xmin<X<xmax and ymin<Y<ymax: cv.onTarget = True
- def Drag(event):
- X = event.x
- Y = event.y
- if cv.onTarget == True:
- if X<0: X=0
- if X>L: X=L
- if Y<0: Y=0
- if Y>H: Y=H
- canvas.coords(target,X-sq,Y-sq,X+sq,Y+sq)
- root = Tk()
- root.title("Drag and Drop")
- L = 480
- H = 160
- sq = 20
- canvas = Canvas(root,width=L,height=H,bg ='white')
- target = canvas.create_rectangle(0,0,sq*2,sq*2,fill='maroon')
- class Cv():
- onTarget = False
- cv = Cv()
- canvas.bind('<Button-1>',Click)
- canvas.bind('<B1-Motion>',Drag)
- canvas.focus_set()
- canvas.pack(padx=10,pady=10)
- root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement