本文实例讲述了Python tkinter事件高级用法。分享给大家供大家参考,具体如下:
先来看看运行效果:
完整实例代码:
# -*- coding:utf-8-*- #! python3 from tkinter import * import threading, time 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) # click canvas.bind('<B1-Motion>', self.onGrow) # and drag canvas.bind('<Double-1>', self.onClear) # delete all canvas.bind('<ButtonPress-3>', self.onMove) # move latest 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] # start dragout self.start = event self.drawn = None def onGrow(self, event): # delete and redraw 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') # use tag all def onMove(self, event): if self.drawn: # move to click spot 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 class CanvasEventsDemoTags(CanvasEventsDemo): def __init__(self, parent=None): CanvasEventsDemo.__init__(self, parent) self.canvas.create_text(100, 8, text='Press o and r to move shapes') self.canvas.master.bind('<KeyPress-o>', self.onMoveOvals) self.canvas.master.bind('<KeyPress-r>', self.onMoveRectangles) self.kinds = self.create_oval_tagged, self.create_rectangle_tagged def create_oval_tagged(self, x1, y1, x2, y2): objectId = self.canvas.create_oval(x1, y1, x2, y2) self.canvas.itemconfig(objectId, tag='ovals', fill='blue') return objectId def create_rectangle_tagged(self, x1, y1, x2, y2): objectId = self.canvas.create_rectangle(x1, y1, x2, y2) self.canvas.itemconfig(objectId, tag='rectangles', fill='red') return objectId def onMoveOvals(self, event): print('moving ovals') self.moveInSquares(tag='ovals') # move all tagged ovals def onMoveRectangles(self, event): print('moving rectangles') self.moveInSquares(tag='rectangles') def moveInSquares(self, tag): # 5 reps of 4 times per sec for i in range(5): for (diffx, diffy) in [(+20, 0), (0, +20), (-20, 0), (0, -20)]: self.canvas.move(tag, diffx, diffy) self.canvas.update() # force screen redraw/update time.sleep(0.25) # pause, but don't block gui class CanvasEventsDemoThread(CanvasEventsDemoTags): def moveEm(self, tag): for i in range(5): for (diffx, diffy) in [(+20, 0), (0, +20), (-20, 0), (0, -20)]: self.canvas.move(tag, diffx, diffy) time.sleep(0.25) # pause this thread only def moveInSquares(self, tag): threading.Thread(self.moveEm, (tag,)).start() if __name__ == '__main__': CanvasEventsDemoThread() mainloop()
更多关于Python相关内容可查看本站专题:《Python数学运算技巧总结》、《Python正则表达式用法总结》、《Python数据结构与算法教程》、《Python函数使用技巧总结》、《Python字符串操作技巧汇总》及《Python入门与进阶经典教程》
希望本文所述对大家Python程序设计有所帮助。
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
暂无评论...
稳了!魔兽国服回归的3条重磅消息!官宣时间再确认!
昨天有一位朋友在大神群里分享,自己亚服账号被封号之后居然弹出了国服的封号信息对话框。
这里面让他访问的是一个国服的战网网址,com.cn和后面的zh都非常明白地表明这就是国服战网。
而他在复制这个网址并且进行登录之后,确实是网易的网址,也就是我们熟悉的停服之后国服发布的暴雪游戏产品运营到期开放退款的说明。这是一件比较奇怪的事情,因为以前都没有出现这样的情况,现在突然提示跳转到国服战网的网址,是不是说明了简体中文客户端已经开始进行更新了呢?
更新日志
2024年11月26日
2024年11月26日
- 凤飞飞《我们的主题曲》飞跃制作[正版原抓WAV+CUE]
- 刘嘉亮《亮情歌2》[WAV+CUE][1G]
- 红馆40·谭咏麟《歌者恋歌浓情30年演唱会》3CD[低速原抓WAV+CUE][1.8G]
- 刘纬武《睡眠宝宝竖琴童谣 吉卜力工作室 白噪音安抚》[320K/MP3][193.25MB]
- 【轻音乐】曼托凡尼乐团《精选辑》2CD.1998[FLAC+CUE整轨]
- 邝美云《心中有爱》1989年香港DMIJP版1MTO东芝首版[WAV+CUE]
- 群星《情叹-发烧女声DSD》天籁女声发烧碟[WAV+CUE]
- 刘纬武《睡眠宝宝竖琴童谣 吉卜力工作室 白噪音安抚》[FLAC/分轨][748.03MB]
- 理想混蛋《Origin Sessions》[320K/MP3][37.47MB]
- 公馆青少年《我其实一点都不酷》[320K/MP3][78.78MB]
- 群星《情叹-发烧男声DSD》最值得珍藏的完美男声[WAV+CUE]
- 群星《国韵飘香·贵妃醉酒HQCD黑胶王》2CD[WAV]
- 卫兰《DAUGHTER》【低速原抓WAV+CUE】
- 公馆青少年《我其实一点都不酷》[FLAC/分轨][398.22MB]
- ZWEI《迟暮的花 (Explicit)》[320K/MP3][57.16MB]