返回顶部

收藏

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. 2016-08-18 10:23:54Mouse button differentiation: center button clic by 蟋蟀MM
  2. 2016-01-11 20:45:29Mouse action: double click by Solon.Ring
  3. 2016-08-18 10:38:40Mouse Double click by Solon.Ring
  4. 2016-10-12 17:51:42Mouse action: Middle Mouse key by xwz
  5. 2013-06-04 13:43:27web.py中自定义jinja2模块 by 赵小全
  6. 2016-08-18 10:53:35Mouse action: left click by 风云轩
  7. 2016-03-15 09:01:35Key event: function key and special key by jack.chen
  8. 2017-03-14 09:02:41Key action: Return key by 山药
  9. 2015-02-23 09:40:23ubuntu安装python3的python-pip by 甄码农
  10. 2016-03-19 18:47:58Key action: Right key by 张宋付
  11. 2016-02-12 09:14:11Capturing clicks in a window by 蟋蟀MM
相关聚客文章
  1. viviworld 发表 2014-11-27 10:00:26 如果学习编程可以重来
  2. jacoxu 发表 2014-12-09 13:19:38 Python IDE使用之Pycharm
  3. 创e 发表 2014-05-27 13:04:59 Django错误日志在命令行显示
  4. Sarike 发表 2012-12-29 09:33:26 让Python脚本暂停执行的几种方法
  5. 博主 发表 2013-10-01 16:00:00 Python 类对象与实例对象源码分析
  6. Gee 发表 2014-10-10 04:57:07 进程管理工具supervisor使用
  7. 博主 发表 2014-12-15 06:12:32 Python爬虫(三)--Coursera抓站小结
  8. 博主 发表 2014-09-23 21:43:22 django单元测试中的Client
  9. 博主 发表 2013-07-18 04:02:00 符合语言习惯的Python编程
  10. rfyiamcool 发表 2014-12-11 04:44:33 使用grafana influxdb构建metric监控系统
  11. 朱春来(Leslie Zhu) 发表 2014-08-28 16:00:00 CherryPy: 一个极简主义Python Web框架(4)——配置
  12. viviworld 发表 2014-12-23 02:39:37 我的100,000小时

发表评论