Advertisement
here2share

# Tk_bind_images.py

Sep 6th, 2016
202
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.32 KB | None | 0 0
  1. # Tk_bind_images.py
  2. from Tkinter import *
  3.  
  4. def startMove(event):
  5.     global startx, starty, item
  6.     widget = event.widget
  7.     startx, starty = widget.winfo_pointerxy()
  8.     x = widget.canvasx(event.x)
  9.     y = widget.canvasy(event.y)
  10.     item = widget.find_closest(x,y)
  11.    
  12. def movingRect(event):
  13.     global startx, starty, item
  14.     widget = event.widget
  15.     newx,newy = widget.winfo_pointerxy()
  16.     diffx = newx - startx
  17.     diffy = newy - starty
  18.     widget.move(item, diffx, diffy)
  19.     widget.update_idletasks()
  20.     startx = newx
  21.     starty = newy
  22.    
  23. r = Tk()
  24.  
  25. c = Canvas(r, width=500, height=500)
  26. c.pack(fill=BOTH, expand=YES)
  27.  
  28. # Draw grid lines every 50 pixels
  29. for x in range (0,500,50):
  30.     c.create_line(x,0,x,500,tags='grid')
  31. for y in range(0,500,50):
  32.     c.create_line(0,y,500,y,tags='grid')
  33.  
  34. # Draw some random rectangles on top
  35. c.create_rectangle(124,124,182,190,fill='#FF0000',outline='#FF0000',tags='rect')
  36. c.create_rectangle(90,50,100,300,fill='#0000FF',outline='#0000FF',tags='rect')
  37. c.create_rectangle(320,210,415,290,fill='#00FF00',outline='#00FF00',tags='rect')
  38. c.create_rectangle(400,50,450,100,fill='#00FFFF',outline='#00FFFF',tags='rect')
  39.  
  40. # bind rect tags to move functions
  41. c.tag_bind('rect', '<Button-1>', startMove)
  42. c.tag_bind('rect', '<Button1-Motion>', movingRect)
  43.  
  44. r.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement