Advertisement
here2share

# Tk_drag_and_drop.py

Sep 6th, 2016
255
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.80 KB | None | 0 0
  1. # Tk_drag_and_drop.py
  2. from Tkinter import *
  3.  
  4. def Click(event):
  5.     X = event.x
  6.     Y = event.y
  7.  
  8.     [xmin,ymin,xmax,ymax] = canvas.coords(target)
  9.     cv.onTarget = False
  10.     if xmin<X<xmax and ymin<Y<ymax: cv.onTarget = True
  11.  
  12. def Drag(event):
  13.     X = event.x
  14.     Y = event.y
  15.  
  16.     if cv.onTarget == True:
  17.         if X<0: X=0
  18.         if X>L: X=L
  19.         if Y<0: Y=0
  20.         if Y>H: Y=H
  21.         canvas.coords(target,X-sq,Y-sq,X+sq,Y+sq)
  22.  
  23. root = Tk()
  24. root.title("Drag and Drop")
  25.  
  26. L = 480
  27. H = 160
  28. sq = 20
  29. canvas = Canvas(root,width=L,height=H,bg ='white')
  30. target = canvas.create_rectangle(0,0,sq*2,sq*2,fill='maroon')
  31.  
  32. class Cv():
  33.     onTarget = False
  34. cv = Cv()
  35. canvas.bind('<Button-1>',Click)
  36. canvas.bind('<B1-Motion>',Drag)
  37.  
  38. canvas.focus_set()
  39. canvas.pack(padx=10,pady=10)
  40.  
  41. root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement