返回顶部

收藏

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. 博主 发表 2009-11-10 08:00:00 Python Mako Template 学习笔记
  2. 博主 发表 2016-08-15 11:54:24 用 Python 和 Asyncio 编写多人在线游戏(一)
  3. 博主 发表 2010-10-07 00:00:00 使用Python编写GIMP插件
  4. 小码哥 发表 2016-08-20 09:26:17 Linux 平台下 Python 脚本编程入门(二)
  5. HX 发表 2013-01-30 12:21:15 快速实现Python C 拓展 - ctypesgen.py
  6. 博主 发表 2014-12-14 00:00:00 Python科学讲座笔记目录
  7. Kingson Zhou 发表 2011-06-13 10:18:01 菜鸟学Python(2) —从一个电话本程序开始
  8. admin 发表 2016-08-29 03:08:33 win+python+selenium实现窗口和tab切换
  9. zhiwei 发表 2013-07-23 04:36:06 从Postgres中读取二进制数据
  10. 博主 发表 2016-04-27 02:04:40 如何卸载通过源码安装的python模块
  11. dccmx 发表 2012-02-18 12:07:06 elFinder配合Tornado
  12. 博主 发表 2013-05-01 06:25:53 Python Tutorial 实践(3)

发表评论