返回顶部

收藏

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. 博主 发表 2010-03-31 05:20:45 Python配置文件部署自动更新脚本教程
  2. admin 发表 2015-07-25 15:53:25 在创建list/tuple时多写一个逗号
  3. 博主 发表 2014-09-21 13:22:46 A Bite of Python
  4. annhe 发表 2015-08-03 10:33:46 Python2 print不换行
  5. rfyiamcool 发表 2015-08-11 15:03:51 报警监控平台之设计url回调webhook管理系统
  6. seanlin0800 发表 2015-08-14 03:39:57 Effective Python 心得筆記: Item 15
  7. 博主 发表 2015-07-28 10:31:06 阅读 coursera-dl 源码
  8. 博主 发表 2015-08-17 00:00:00 Python Arduino Distance
  9. 比尔盖子 发表 2015-08-19 07:39:04 为什么 os.path 可以被直接导入
  10. 秋水逸冰 发表 2014-03-06 07:09:35 Eclipse下搭建Python开发环境
  11. 归根落叶 发表 2015-08-29 07:24:31 接口自动化测试方案详解
  12. rainy 发表 2015-09-02 15:52:26 网页正文及内容图片提取算法

发表评论