Start New Discussion within our Software Development Community

Not a mod of code linked above, but it does what I was asking for:

import Tkinter as tk

class SnappingCanvas(tk.Canvas):
    ''' A canvas that bites! ;-)'''
    def __init__(self, master, **kw):
        self.click = None
        tk.Canvas.__init__(self, master, **kw)
        
        #for x in range (50,300,50): 
        #    self.create_line(x,0, x,500, tags='grid')
        #    self.create_line(0,x, 500,x, tags='grid')
            
        self.create_rectangle(0,0, 50,50, fill="green",
                              activefill='red', tag="R")
        self.tag_bind("R", "<Button-1>", self.onClick1)
        self.tag_bind("R", "<B1-Motion>", self.onMotion1)
        self.tag_bind("R", "<ButtonRelease-1>",self.onRelease1)

    def onClick1(self, event):
        self.click = event.x, event.y
        self.delete('cpoint')
        
    def onMotion1(self, event):
        x, y = self.click
        dx = event.x - x
        dy = event.y - y
        self.move('current' ,dx ,dy)
        self.click = event.x, event.y

    def onRelease1(self, event):
        #sx, sy, ex, ey = self.coords("R")
        #mx, my = (sx+ex)/2.0 , (sy+ey)/2.0
        mx, my = 100, 100 # this tells image where to snap into place
        x, y = mx-(mx%50), my-(my%50)
        self.coords("R", x,y, x+50,y+50)
        #self.coords("R", x,y, x+50,y+50)
        #self.create_oval(mx-2, my-2, mx+2, my+2, fill='magenta',
                         #tags='cpoint')


root = tk.Tk()
snapit = SnappingCanvas(root, width=300, height=300, bg="white")
snapit.pack()
root.mainloop()
This question has already been answered. Start a new discussion instead.