tkinter事件高级用法实例

简介:   from tkinter import * import threading, time trace = 0 class CanvasEventsDemo: def __init__(self, parent=None): canvas =...

 

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(75, 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()

 

目录
相关文章
|
7月前
|
iOS开发 MacOS
LabVIEW如何使用热键去触发自定义的事件
LabVIEW如何使用热键去触发自定义的事件
150 1
|
7月前
|
Python
【python自动化】Playwright基础教程(五)事件操作②悬停&输入&清除精讲
【python自动化】Playwright基础教程(五)事件操作②悬停&输入&清除精讲
179 0
|
7月前
|
开发框架 算法 程序员
【Qt 元对象系统】深入探索Qt事件过滤:从基础到高级应用
【Qt 元对象系统】深入探索Qt事件过滤:从基础到高级应用
212 7
Echarts实战案例代码(17):鼠标操作事件的代码
Echarts实战案例代码(17):鼠标操作事件的代码
179 0
|
安全 UED Python
【100天精通python】Day36:GUI界面编程_Tkinter高级功能操作和示例
【100天精通python】Day36:GUI界面编程_Tkinter高级功能操作和示例
253 0
|
Python 容器
Python Tkinter教程(一)——tkinter编程基本步骤、窗口基本属性及Toplevel控件的使用
Python Tkinter教程(一)——tkinter编程基本步骤、窗口基本属性及Toplevel控件的使用
272 1
|
索引
PyQt5 技术篇-QComboBox下拉框绑定监听事件实例演示
PyQt5 技术篇-QComboBox下拉框绑定监听事件实例演示
1120 0
|
JavaScript 前端开发
Javascript中常用事件集合和事件使用方法
Javascript中常用事件集合和事件使用方法
Javascript中常用事件集合和事件使用方法
从零开始学Pyqt5之【信号与槽函数定义与使用方法:包含自定义信号与自定义槽函数】
从零开始学Pyqt5之【信号与槽函数定义与使用方法:包含自定义信号与自定义槽函数】