draw elastic shapes on a canvas on drag, move on right click; : Canvas « Tkinker « Python Tutorial






draw elastic shapes on a canvas on drag, move on right click;
from Tkinter import *
trace = 0 
     
class CanvasEventsDemo: 
    def __init__(self, parent=None):
        canvas = Canvas(width=300, height=300, bg='white') 
        canvas.pack()
        canvas.bind('<ButtonPress-1>', self.onStart)  
        canvas.bind('<B1-Motion>',     self.onGrow)   
        canvas.bind('<Double-1>',      self.onClear)  
        canvas.bind('<ButtonPress-3>', self.onMove)   
        self.canvas = canvas
        self.drawn  = None

    def onStart(self, event):
        self.start = event
        self.drawn = None

    def onGrow(self, event):                          
        canvas = event.widget
        if self.drawn: canvas.delete(self.drawn)
        objectId = canvas.create_oval(self.start.x, self.start.y, event.x, event.y)
        if trace: print objectId
        self.drawn = objectId

    def onClear(self, event):
        event.widget.delete('all')

    def onMove(self, event):
        if self.drawn:            
            if trace: print self.drawn
            canvas = event.widget
            diffX, diffY = (event.x - self.start.x), (event.y - self.start.y)
            canvas.move(self.drawn, diffX, diffY)
            self.start = event
     
CanvasEventsDemo()
mainloop()








18.5.Canvas
18.5.1.Add CheckButon to frameAdd CheckButon to frame
18.5.2.Canvas paint program.Canvas paint program.
18.5.3.Add Label to canvasAdd Label to canvas
18.5.4.Draw lineDraw line
18.5.5.Change line colorChange line color
18.5.6.Draw ovalDraw oval
18.5.7.Draw arcDraw arc
18.5.8.Draw rectangleDraw rectangle
18.5.9.Draw textDraw text
18.5.10.Add gif image to canvas
18.5.11.Canvas click eventCanvas click event
18.5.12.draw elastic shapes on a canvas on drag, move on right click;draw elastic shapes on a canvas on drag, move on right click;
18.5.13.Scrolled canvasScrolled canvas
18.5.14.Add double click action to canvasAdd double click action to canvas