Python tkinter事件高级用法实例

yipeiwu_com5年前Python基础

本文实例讲述了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程序设计有所帮助。

相关文章

将Pytorch模型从CPU转换成GPU的实现方法

最近将Pytorch程序迁移到GPU上去的一些工作和思考 环境:Ubuntu 16.04.3 Python版本:3.5.2 Pytorch版本:0.4.0 0. 序言 大家知道,在深度学...

python selenium 执行完毕关闭chromedriver进程示例

因为使用多次以后发现进程中出现了很多chromedriver的残留,造成卡顿,所以决定优化一下。 这个问题困扰了楼主很久,百度谷歌查来查去都只有java,后面根据java和seleniu...

使用Python给头像戴上圣诞帽的图像操作过程解析

使用Python给头像戴上圣诞帽的图像操作过程解析

前言 随着圣诞的到来,大家纷纷@官方微信给自己的头像加上一顶圣诞帽。当然这种事情用很多P图软件都可以做到。但是作为一个学习图像处理的技术人,还是觉得我们有必要写一个程序来做这件事情。而且...

给我一面国旗 python帮你实现

给我一面国旗 python帮你实现

本文实例为大家分享了Python之给我一面国旗的具体代码,供大家参考,具体内容如下 1、“给我一面国旗@微信官方” 今天“给我一面国旗@微信官方”刷爆了朋友圈,我也蹭波热度,出个Pyth...

python数据处理实战(必看篇)

python数据处理实战(必看篇)

一、运行环境 1、python版本 2.7.13 博客代码均是这个版本 2、系统环境:win7 64位系统 二、需求 对杂乱文本数据进行处理 部分数据截图如下,第一个字段是原字段,后面3...