返回顶部

收藏

Move what you drew on a canvas

更多
from Tkinter import *
import time

trace = 0 

class CanvasEventsDemo: 
    def __init__(self, parent=None):
        canvas = Canvas(width=300, height=300, bg='beige') 
        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
        self.kinds = [canvas.create_oval, canvas.create_rectangle]
    def onStart(self, event):
        self.shape = self.kinds[0]
        self.kinds = self.kinds[1:] + self.kinds[:1]      
        self.start = event
        self.drawn = None
    def onGrow(self, event):                     
        canvas = event.widget
        if self.drawn: canvas.delete(self.drawn)
        objectId = self.shape(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

class CanvasEventsDemoTags(CanvasEventsDemo):
    def __init__(self, parent=None):
        CanvasEventsDemo.__init__(self, parent)
        self.canvas.create_text(75, 8, text='Press o and r to move shapes')
        self.canvas.master.bind('<KeyPress-o>', self.onMoveOvals)    
        self.canvas.master.bind('<KeyPress-r>', self.onMoveRectangles)  
        self.kinds = self.create_oval_tagged, self.create_rectangle_tagged
    def create_oval_tagged(self, x1, y1, x2, y2):
        objectId = self.canvas.create_oval(x1, y1, x2, y2)
        self.canvas.itemconfig(objectId, tag='ovals', fill='blue')
        return objectId
    def create_rectangle_tagged(self, x1, y1, x2, y2):
        objectId = self.canvas.create_rectangle(x1, y1, x2, y2)
        self.canvas.itemconfig(objectId, tag='rectangles', fill='red')
        return objectId
    def onMoveOvals(self, event):
        print 'moving ovals'
        self.moveInSquares(tag='ovals')          
    def onMoveRectangles(self, event):
        print 'moving rectangles'
        self.moveInSquares(tag='rectangles')
    def moveInSquares(self, tag):                
        for i in range(5):
            for (diffx, diffy) in [(+20, 0), (0, +20), (-20, 0), (0, -20)]:
                self.canvas.move(tag, diffx, diffy)
                self.canvas.update()             
                time.sleep(0.25)                 

if __name__ == '__main__':
    CanvasEventsDemoTags()
    mainloop()

标签:Python,Event

收藏

0人收藏

支持

0

反对

0

相关聚客文章
  1. 博主 发表 2013-11-04 00:00:00 deploy python on nginx
  2. 代码猴 发表 2014-12-30 05:56:00 python 打包压缩文件
  3. 仙宇 发表 2018-03-31 16:21:26 【Python搞搞轻量Blog】第二发 Flask入门(2)
  4. techug 发表 2014-12-30 10:05:47 Python语言在企业级应用上的十大谬误
  5. Jakub Pilimon 发表 2018-04-11 21:01:27 Event Storming and Spring with a Splash of DDD
  6. aGuegu 发表 2014-07-07 03:57:46 [python] cx_Oracle 在 virtualenv 中的 pip 安装
  7. 船长 发表 2018-04-23 16:00:00 如何用TensorFlow追踪《星战》里的千年隼飞船?
  8. 博主 发表 2012-08-25 22:13:00 Python Tips
  9. 博主 发表 2015-08-06 16:00:00 在C中调用Python程序(II)
  10. 博主 发表 2013-03-26 00:00:00 python排序
  11. linux@linux.cn (linu 发表 2018-05-22 08:13:09 6 个 Python 的日期时间库
  12. 博主 发表 2015-01-11 08:10:00 Python Universal Newlines