如何判斷 Tkinter 中的按鈕何時釋放?
在 Tkinter 中,事件通常由按鈕或鍵呼叫。每當使用者按下分配的鍵或單擊分配的按鈕時,事件便會執行。為了執行事件,我們可以將按鈕或鍵與回撥函式繫結。
考慮一個應用程式,在其中我們需要在釋放滑鼠按鈕時觸發事件。這可以透過在 bind(<Button Release>, callback) 方法中傳遞 <Button Release> 引數來實現。
示例
# 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 on mouse button clicked
def on_click(event):
label["text"]="Hello, There!"
def on_release(event):
label["text"]="Button Released!"
# Crate a Label widget
label=Label(win, text="Click anywhere..", font=('Calibri 18 bold'))
label.pack(pady=60)
win.bind("<ButtonPress-1>", on_click)
win.bind("<ButtonRelease-1>", on_release)
win.mainloop()輸出
如果我們執行以上程式碼,它將顯示一個帶有標籤小部件的視窗。

現在,單擊視窗中的任意位置以檢視螢幕上的訊息,該訊息將在我們釋放滑鼠按鈕時更新。

當您釋放滑鼠按鈕時,它將顯示以下訊息 -
廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP