Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Tk_snap2grid.py
- import tkinter as tk
- def Snap2Grid(width=300, height=300):
- canvas.config(width=width, height=height)
- for x in range(1,width,50):
- canvas.create_line(x,0, x,height)
- for y in range(1,height,50):
- canvas.create_line(0,y, width,y)
- canvas.create_rectangle(51,51, 101,101, fill="red",
- activefill='green', tag="R")
- 0
- root = tk.Tk()
- canvas = tk.Canvas()
- canvas.pack()
- canvas.click = None
- def onClick1(event):
- canvas.click = event.x, event.y
- canvas.delete('cpoint')
- def onMotion1(event):
- x, y = canvas.click
- dx = event.x - x
- dy = event.y - y
- canvas.move('current' ,dx ,dy)
- canvas.click = event.x, event.y
- def onRelease1(event):
- mx, my = event.x, event.y
- x, y = mx-(mx%50)+1, my-(my%50)+1
- canvas.coords("R", x,y, x+50,y+50)
- canvas.tag_bind("R", "<Button-1>", onClick1)
- canvas.tag_bind("R", "<B1-Motion>", onMotion1)
- canvas.tag_bind("R", "<ButtonRelease-1>",onRelease1)
- Snap2Grid()
- root.mainloop()
Add Comment
Please, Sign In to add comment