91超碰碰碰碰久久久久久综合_超碰av人澡人澡人澡人澡人掠_国产黄大片在线观看画质优化_txt小说免费全本

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

Python tkinter事件高級用法實例

發布時間:2020-09-06 23:34:16 來源:腳本之家 閱讀:175 作者:羅兵 欄目:開發技術

本文實例講述了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程序設計有所幫助。

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

江永县| 富川| 牡丹江市| 高平市| 邹平县| 鹤山市| 尼木县| 赤峰市| 龙南县| 达拉特旗| 香河县| 迭部县| 惠来县| 游戏| 竹山县| 固原市| 青岛市| 霍城县| 磐安县| 通山县| 古丈县| 新竹县| 大同县| 内黄县| 永安市| 博爱县| 龙山县| 巨野县| 咸丰县| 海阳市| 招远市| 抚顺县| 长子县| 白河县| 绥江县| 德昌县| 青岛市| 微博| 巫山县| 浙江省| 临猗县|