返回顶部

收藏

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. saviio 发表 2014-06-27 17:25:58 「谈谈」:BigPipe Solution for Python / Node.js / C#
  2. chirs <> 发表 2012-02-23 01:35:05 udpip: 用UDP封装IP数据包建立VPN
  3. 博主 发表 2015-01-12 06:10:51 开始python-flask的学习
  4. hushlight 发表 2015-04-16 02:29:28 Leetcode – Palindrome Partitioning II
  5. 博主 发表 2015-03-22 00:00:00 用Python 分析股票数据
  6. 博主 发表 2013-04-14 16:00:00 Python Tutorial 实践(2)
  7. daimin 发表 2013-09-14 00:00:00 用python进行博客的迁移
  8. 博主 发表 2015-04-03 07:13:29 python-访问父类方法
  9. 博主 发表 2015-03-06 11:40:51 Python练习 (一)
  10. Tommy 发表 2014-12-17 08:00:06 使用python抓取搞笑图片插入到Mysql中 顺便解决乱码问题
  11. 博主 发表 2014-11-27 03:20:44 Python发送邮件
  12. Markjour 发表 2014-07-10 02:07:47 Python 文件系统

发表评论