返回顶部

收藏

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. 阿亚 发表 2014-02-01 06:54:09 Python-pip, RubyGems, node-npm使用国内镜像加速下载
  2. 博主 发表 2017-03-10 02:52:18 Don't forget your sets in Python!
  3. tenking 发表 2014-07-15 15:24:14 一些面试题(Python)
  4. 博主 发表 2017-03-17 05:05:06 flask 源码解析:session
  5. 0X55AA 发表 2014-07-22 13:23:03 whitespace的python实现
  6. 博主 发表 2017-03-23 09:03:19 荐书:《Fluent Python》
  7. 博主 发表 2014-01-11 19:58:23 如何编写高质量的python程序
  8. zhu329599788@126 发表 2017-03-30 04:24:06 Python标准异常和异常处理详解
  9. fmnisme 发表 2014-08-02 07:15:20 saltstack应用之简化pillar配置步骤
  10. 胡博强.doc 发表 2017-04-08 16:00:00 贝叶斯方法的概率编程与推断——贝叶斯推断的哲♂学
  11. Armin Ronacher 发表 2014-08-16 00:00:00 The Python I Would Like To See
  12. 博主 发表 2015-03-12 00:00:00 python读取excel

发表评论