Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Tk_drag_objects.py
- from Tkinter import *
- root=Tk()
- canvas=Canvas(root)
- _drag_data={"x": 0, "y": 0, "item": None, "color": None, "OLcolor": None}
- def obj_ButtonPress(event):
- _drag_data["item"]=canvas.find_closest(event.x, event.y)[0]
- _drag_data["x"]=event.x
- _drag_data["y"]=event.y
- _drag_data["color"]=canvas.itemcget(_drag_data["item"], "fill")
- _drag_data["OLcolor"]=canvas.itemcget(_drag_data["item"], "outline")
- canvas.itemconfig(_drag_data["item"], outline="black", fill="grey")
- canvas.tag_raise(_drag_data["item"])
- def obj_ButtonPress2(event):
- _drag_data["item"]=canvas.find_closest(event.x, event.y)[0]
- canvas.tag_lower(_drag_data["item"])
- def obj_ButtonRelease(event):
- canvas.itemconfig(_drag_data["item"], fill=_drag_data["color"])
- canvas.itemconfig(_drag_data["item"], outline=_drag_data["OLcolor"])
- def obj_Motion(event):
- delta_x=event.x - _drag_data["x"]
- delta_y=event.y - _drag_data["y"]
- canvas.move(_drag_data["item"], delta_x, delta_y)
- _drag_data["x"]=event.x
- _drag_data["y"]=event.y
- def _create_token_(coord, color):
- (x,y)=coord
- canvas.create_oval(x-30, y-30, x+30, y+30,
- outline=color, fill=color, tags="obj_")
- canvas.create_rectangle(0,100, 200, 200, outline='yellow', fill='yellow', tags="obj_")
- canvas.create_oval(0,0, 100,100, outline='red', fill='red', tags="obj_")
- canvas.create_polygon(111,50, 200,0, 200,100, outline='green', fill='green', tags="obj_")
- _create_token_((100, 100), "purple")
- _create_token_((200, 100), "black")
- canvas.tag_bind("obj_", "<ButtonPress-1>", obj_ButtonPress)
- canvas.tag_bind("obj_", "<ButtonPress-3>", obj_ButtonPress2)
- canvas.tag_bind("obj_", "<ButtonRelease-1>", obj_ButtonRelease)
- canvas.tag_bind("obj_", "<B1-Motion>", obj_Motion)
- canvas.pack()
- root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement