所有 Tkinter 事件列表
Tkinter 是一個 Python 庫,用於建立基於 GUI 的應用程式。Tkinter 帶有很多內建功能和擴充套件,可用於最佳化應用程式效能和行為。Tkinter 事件通常用於提供一個充當使用者和應用程式邏輯之間橋樑的介面。我們可以在任何 Tkinter 應用程式中使用事件,使其可操作且功能齊全。
以下列出了一些常用的 Tkinter 事件,這些事件通常用於使應用程式具有互動性。
- <Button> − 在處理程式中使用 Button 事件來繫結滑鼠滾輪和按鈕。
- <ButtonRelease> − 除了點選按鈕,您還可以透過釋放滑鼠按鈕來觸發事件。
- <Configure> − 使用此事件更改小部件的屬性。
- Destroy − 使用此事件來殺死或終止特定的小部件。
- <Enter> − 它實際上就像 <return> 事件一樣,可用於使用滑鼠指標將焦點放在小部件上。
- <Expose> − 每當小部件或應用程式的某些部分變得可見(之前被應用程式中的另一個視窗覆蓋)時,就會發生此事件。
- <Focus In> − 此事件通常用於將焦點放在特定的小部件上。
- <Focus Out> − 將焦點從當前小部件移開。
- <Key Press> − 按下鍵開始程序或呼叫處理程式。
- <KeyRelease> − 透過釋放鍵開始程序或呼叫事件。
- <Leave> − 當用戶從一個小部件切換到另一個小部件時,使用此事件跟蹤滑鼠指標。
- <Map> − 使用 Map 事件在應用程式中顯示或顯示任何小部件。
- <Motion> − 每當滑鼠指標在應用程式內完全移動時,跟蹤事件。
- <Unmap> − 可以從應用程式中取消對映小部件。這類似於使用 grid_remove() 隱藏小部件。
- <Visibility> − 如果應用程式的某些部分在螢幕上變得可見,則可能會發生事件。
示例
在此示例中,我們使用 <Button> 事件來顯示每當按下滑鼠按鈕時的小部件。
# Import the Required libraries from tkinter import * # Create an instance of tkinter frame or window win= Tk() # Set the size of the window win.geometry("700x350") # Define a function to display the message def display_text(e): label.config(text="Code never lies, comments sometimes do", font=('Helvetica 17 bold')) # Create a label widget to add some text label= Label(win, text= "") label.pack(pady= 50) # Bind the Mouse button event win.bind('<Button-1>',display_text) win.mainloop()
輸出
執行上述 Python 指令碼將顯示一個空視窗。左鍵單擊視窗將在視窗上顯示一些訊息。
廣告