返回顶部

收藏

Use mouse to draw a shape on canvas

更多
from Tkinter import *
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

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

标签:Python,Event

收藏

0人收藏

支持

0

反对

0

相关聚客文章
  1. Young Joy 发表 2013-03-28 07:35:16 在SAE中django执行manage.py
  2. 博主 发表 2015-04-21 09:05:15 python做爬虫的常见问题
  3. 奈何col 发表 2015-04-27 18:37:13 Flask-MongoEngine
  4. 博主 发表 2015-01-04 00:00:00 python的命令行参数解析利器optionParser
  5. 博主 发表 2014-05-17 02:51:53 Python 简单的统计基类,同名变量和字符串的使用
  6. hushlight 发表 2015-04-23 10:56:08 Leetcode – Merge k Sorted Lists
  7. eliteGoblin&#39;s Bl 发表 2013-05-04 15:11:00 Event-Log _2013年04月篇 - eliteGoblin's Blog
  8. 博主 发表 2015-03-03 14:14:36 python实现代码行数统计
  9. atolin 发表 2012-11-13 09:04:04 Linux下C++开发Python扩展模块
  10. imnotadmin 发表 2013-08-30 14:11:01 python多线程学习1
  11. 博主 发表 2011-03-22 07:00:00 Python 中的生成器(generator)
  12. 博主 发表 2013-11-23 08:00:00 Python class 入门

发表评论