Tkinter中常用的函数
生活随笔
收集整理的這篇文章主要介紹了
Tkinter中常用的函数
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
Tkinter中常用的函數(shù)
- 窗口管理器的刷新事件
窗口管理器的刷新事件
ps:一個Tkinter應用在絕大部分時間都花費在內部的時間循環(huán)上(通過mainloop方法進入)。事件來自于各種途徑。包括來自用戶的按鍵和鼠標操作,窗口管理器的刷新事件(大多數(shù)情況下由用戶直接觸發(fā))。Tkinter提供了一個有效的機制去處理事件。對于每一個組件,都可以把Python功能和方法映和事件映射在一起。 widget.bind(event, handler)eg:
from tkinter import *root = Tk() def callback(event):print("clicked at", event.x, event.y)frame = Frame(root, width=100, height=100) frame.bind("<Button-1>", callback) frame.pack() root.mainloop()In this example, we use the bind method of the frame widget to bind a callback function to an event called . Run this program and click in the window that appears. Each time you click, a message like “clicked at 44 63” is printed to the console window.
在這個例子里,使用了frame組件的bind方法去把一個callback函數(shù)和一個叫做的時間映射在一起。運行這個程序,并行在出現(xiàn)的窗口中點擊,每點擊一次,一條類似"clicked at 44 63"的信息就會被打印到控制臺窗口。
鍵盤事件將會被發(fā)送到當前擁有鍵盤焦點的組件中。focus_set方法可以把鍵盤焦點設置到一個組件上。
from Tkinter import *root = Tk()def key(event):print "pressed", repr(event.char)def callback(event):frame.focus_set()print "clicked at", event.x, event.yframe = Frame(root, width=100, height=100) frame.bind("<Key>", key) frame.bind("<Button-1>", callback) frame.pack()root.mainloop() 《新程序員》:云原生和全面數(shù)字化實踐50位技術專家共同創(chuàng)作,文字、視頻、音頻交互閱讀總結
以上是生活随笔為你收集整理的Tkinter中常用的函数的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: linux:命令常用操作
- 下一篇: python实现STM32单片机通信