Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Tk_bind_images.py
- from Tkinter import *
- def startMove(event):
- global startx, starty, item
- widget = event.widget
- startx, starty = widget.winfo_pointerxy()
- x = widget.canvasx(event.x)
- y = widget.canvasy(event.y)
- item = widget.find_closest(x,y)
- def movingRect(event):
- global startx, starty, item
- widget = event.widget
- newx,newy = widget.winfo_pointerxy()
- diffx = newx - startx
- diffy = newy - starty
- widget.move(item, diffx, diffy)
- widget.update_idletasks()
- startx = newx
- starty = newy
- r = Tk()
- c = Canvas(r, width=500, height=500)
- c.pack(fill=BOTH, expand=YES)
- # Draw grid lines every 50 pixels
- for x in range (0,500,50):
- c.create_line(x,0,x,500,tags='grid')
- for y in range(0,500,50):
- c.create_line(0,y,500,y,tags='grid')
- # Draw some random rectangles on top
- c.create_rectangle(124,124,182,190,fill='#FF0000',outline='#FF0000',tags='rect')
- c.create_rectangle(90,50,100,300,fill='#0000FF',outline='#0000FF',tags='rect')
- c.create_rectangle(320,210,415,290,fill='#00FF00',outline='#00FF00',tags='rect')
- c.create_rectangle(400,50,450,100,fill='#00FFFF',outline='#00FFFF',tags='rect')
- # bind rect tags to move functions
- c.tag_bind('rect', '<Button-1>', startMove)
- c.tag_bind('rect', '<Button1-Motion>', movingRect)
- r.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement