Advertisement
here2share

# Tk_snaptext2grid.py

Nov 7th, 2019
254
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.11 KB | None | 0 0
  1. # Tk_snaptext2grid.py
  2.  
  3. import Tkinter as tk
  4.  
  5. def Snap2Grid(width=300, height=300):
  6.     canvas.config(width=width, height=height)
  7.     def onClick1(event):
  8.         canvas.click = event.x, event.y
  9.         canvas.delete('cpoint')
  10.     def onMotion1(event):
  11.         x, y = canvas.click
  12.         dx = event.x - x
  13.         dy = event.y - y
  14.         canvas.move('current' ,dx ,dy)
  15.         canvas.click = event.x, event.y
  16.     def onRelease1(event):
  17.         mx, my = event.x, event.y
  18.         x, y = max(0, mx-(mx%150))+75, my-(my%30)+16
  19.         canvas.coords("R", x,y)
  20.     canvas.create_line(width/2,0, width/2,height)
  21.     for y in range(1,height,30):
  22.         canvas.create_line(0,y, width,y)
  23.     canvas.create_text(150, 60, text='drag this text', font=('arial', 18),
  24.             fill="red", activefill='green', tag="R")
  25.     canvas.tag_bind("R", "<Button-1>", onClick1)
  26.     canvas.tag_bind("R", "<B1-Motion>", onMotion1)
  27.     canvas.tag_bind("R", "<ButtonRelease-1>",onRelease1)
  28. 0
  29. root = tk.Tk()
  30. canvas = tk.Canvas()
  31. canvas.pack()
  32. canvas.click = None
  33. Snap2Grid()
  34.  
  35. '''
  36. if text in c.find_overlapping(str(x-10), str(y-10), str(x+10), str(y+10)):
  37.     c.coords(text, x, y)  # move text to mouse position
  38. '''
  39.  
  40. root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement