Python tkinter事件高级用法实例
作者:罗兵 时间:2023-12-09 10:28:17
本文实例讲述了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程序设计有所帮助。
来源:http://www.cnblogs.com/hhh5460/p/5188527.html
标签:Python,tkinter
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
MySQL索引的一些常见面试题大全(2022年)
2024-01-13 00:17:30
![](https://img.aspxhome.com/file/2023/8/111378_0s.jpg)
本机安装PaddlePaddle安装指南及步骤详解
2023-12-18 19:35:57
![](https://img.aspxhome.com/file/2023/3/70773_0s.jpg)
selenium+python自动化测试之环境搭建
2022-05-15 13:51:32
![](https://img.aspxhome.com/file/2023/1/75901_0s.png)
PYTHON 中使用 GLOBAL引发的一系列问题
2023-07-08 16:24:21
vue实现给div绑定keyup的enter事件
2024-05-13 09:08:51
![](https://img.aspxhome.com/file/2023/4/126644_0s.jpg)
vue项目页面的打印和下载PDF加loading效果的实现(加水印)
2024-04-30 10:29:39
![](https://img.aspxhome.com/file/2023/3/130173_0s.jpg)
利用django和mysql实现一个简单的web登录页面
2024-01-13 18:16:16
![](https://img.aspxhome.com/file/2023/4/133514_0s.png)
HTML+CSS 模仿Windows 7 桌面效果
2010-06-17 14:33:00
![](https://img.aspxhome.com/file/UploadPic/20106/17/1044050-15s.png)
python完成FizzBuzzWhizz问题(拉勾网面试题)示例
2022-01-31 14:41:11
ASP 隐藏下载地址及防盗链代码
2011-02-26 11:17:00
Python监听剪切板实现方法代码实例
2023-11-10 13:48:32
MySQL DBA教程:Mysql性能优化之缓存参数优化
2024-01-21 19:11:56
Python Selenium参数配置方法解析
2023-12-28 09:20:02
浅谈Python中的数据类型
2022-11-29 06:19:22
PHP在网页中动态生成PDF文件详细教程
2024-05-08 09:34:56
![](https://img.aspxhome.com/file/2023/8/129818_0s.jpg)
python面向对象 反射原理解析
2021-05-14 08:56:32
通过实例了解JS 连续赋值
2024-05-02 16:15:14
![](https://img.aspxhome.com/file/2023/4/132864_0s.png)
详解Python中的变量及其命名和打印
2023-07-23 11:31:45
PHP+MYSQL实现读写分离简单实战
2023-11-23 21:30:42
javascript轻量级库createjs使用Easel实现拖拽效果
2024-05-22 10:41:16