Advertisement
here2share

# Tk_drag_objects.py

Sep 30th, 2016
313
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.76 KB | None | 0 0
  1. # Tk_drag_objects.py
  2.  
  3. from Tkinter import *
  4. root=Tk()
  5. canvas=Canvas(root)
  6.  
  7. _drag_data={"x": 0, "y": 0, "item": None, "color": None, "OLcolor": None}
  8.  
  9. def obj_ButtonPress(event):
  10.     _drag_data["item"]=canvas.find_closest(event.x, event.y)[0]
  11.     _drag_data["x"]=event.x
  12.     _drag_data["y"]=event.y
  13.     _drag_data["color"]=canvas.itemcget(_drag_data["item"], "fill")
  14.     _drag_data["OLcolor"]=canvas.itemcget(_drag_data["item"], "outline")
  15.     canvas.itemconfig(_drag_data["item"], outline="black", fill="grey")
  16.     canvas.tag_raise(_drag_data["item"])
  17.  
  18. def obj_ButtonPress2(event):
  19.     _drag_data["item"]=canvas.find_closest(event.x, event.y)[0]
  20.     canvas.tag_lower(_drag_data["item"])
  21.  
  22. def obj_ButtonRelease(event):
  23.     canvas.itemconfig(_drag_data["item"], fill=_drag_data["color"])
  24.     canvas.itemconfig(_drag_data["item"], outline=_drag_data["OLcolor"])
  25.  
  26. def obj_Motion(event):
  27.     delta_x=event.x - _drag_data["x"]
  28.     delta_y=event.y - _drag_data["y"]
  29.     canvas.move(_drag_data["item"], delta_x, delta_y)
  30.     _drag_data["x"]=event.x
  31.     _drag_data["y"]=event.y
  32.  
  33. def _create_token_(coord, color):
  34.     (x,y)=coord
  35.     canvas.create_oval(x-30, y-30, x+30, y+30,
  36.                             outline=color, fill=color, tags="obj_")
  37.  
  38. canvas.create_rectangle(0,100, 200, 200, outline='yellow', fill='yellow', tags="obj_")
  39. canvas.create_oval(0,0, 100,100, outline='red', fill='red', tags="obj_")
  40. canvas.create_polygon(111,50, 200,0, 200,100, outline='green', fill='green', tags="obj_")
  41.  
  42. _create_token_((100, 100), "purple")
  43. _create_token_((200, 100), "black")
  44.  
  45. canvas.tag_bind("obj_", "<ButtonPress-1>", obj_ButtonPress)
  46. canvas.tag_bind("obj_", "<ButtonPress-3>", obj_ButtonPress2)
  47. canvas.tag_bind("obj_", "<ButtonRelease-1>", obj_ButtonRelease)
  48. canvas.tag_bind("obj_", "<B1-Motion>", obj_Motion)
  49.  
  50. canvas.pack()
  51.  
  52. root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement