返回顶部

收藏

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. 博主 发表 2014-12-05 06:53:35 Python面向对象
  2. dormy 发表 2014-12-11 07:51:15 python去/替标点总结
  3. 博主 发表 2014-12-15 00:00:00 1.1科学计算工具及流程-Python科学讲座笔记
  4. TonySeek 发表 2013-02-04 19:58:00 记录一些坑
  5. 博主 发表 2014-07-17 16:00:00 Python 核心编程读书笔记 Day7
  6. admin 发表 2014-12-17 13:31:24 python获取和自动插入wordpress tags标签
  7. Liuyang Wan (t@sfdye 发表 2014-04-04 00:00:00 管理多Python环境之Virtualenv与Virtualenvwrapper
  8. admin 发表 2014-12-18 09:42:38 python实现html代码转义转换
  9. 博主 发表 2013-03-22 04:47:36 Python与C++联合编程的简介
  10. wolfking 发表 2013-11-10 10:51:26 优雅地使用python
  11. 朱春来(Leslie Zhu) 发表 2014-11-11 16:00:00 如何组织Python软件包
  12. admin 发表 2014-06-12 01:40:15 Python中如何判断变量是否已经定义

发表评论