未加星标

Python tkinter事件高级用法实例

字体大小 | |
[开发(python) 所属分类 开发(python) | 发布者 店小二04 | 时间 | 作者 红领巾 ] 0人收藏点击收藏

本文实例讲述了python tkinter事件高级用法。分享给大家供大家参考,具体如下:

先来看看运行效果:

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程序设计有所帮助。


您可能感兴趣的文章:python3使用tkinter实现ui界面简单实例Python Tkinter简单布局实例教程python的tkinter布局之简单的聊天窗口实现方法Python Tkinter基础控件用法Python中使用Tkinter模块创建GUI程序实例python3.5使用tkinter制作记事本python使用Tkinter显示网络图片的方法Python tkinter模块弹出窗口及传值回到主窗口操作详解python3.3使用tkinter开发猜数字游戏示例基于python的Tkinter实现一个简易计算器Python基于Tkinter实现的记事本实例

本文开发(python)相关术语:python基础教程 python多线程 web开发工程师 软件开发工程师 软件开发流程

tags: self,canvas,Python,event,def,tag,objectId,drawn,tkinter,start,move,gt,Tkinter,kinds
分页:12
转载请注明
本文标题:Python tkinter事件高级用法实例
本站链接:http://www.codesec.net/view/572300.html
分享请点击:


1.凡CodeSecTeam转载的文章,均出自其它媒体或其他官网介绍,目的在于传递更多的信息,并不代表本站赞同其观点和其真实性负责;
2.转载的文章仅代表原创作者观点,与本站无关。其原创性以及文中陈述文字和内容未经本站证实,本站对该文以及其中全部或者部分内容、文字的真实性、完整性、及时性,不作出任何保证或承若;
3.如本站转载稿涉及版权等问题,请作者及时联系本站,我们会及时处理。
登录后可拥有收藏文章、关注作者等权限...
技术大类 技术大类 | 开发(python) | 评论(0) | 阅读(42)