Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Python

วิธีตรวจจับการปล่อยปุ่มเมาส์ (Button Release) ใน Tkinter


ใน Tkinter events เรียกโดยปุ่มหรือปุ่มกด เมื่อผู้ใช้กดปุ่มที่กำหนด หรือคลิกปุ่มที่กำหนด events จะทำงาน ใช้ bind() ผูกปุ่มหรือปุ่มกดกับ callback function

พิจารณาแอปพลิเคชันที่ต้องการ trigger event เมื่อปล่อยปุ่มเมาส์ ทำได้โดยส่งพารามิเตอร์ <Button Release> ใน bind(<ButtonRelease>, callback)

ตัวอย่าง

# 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("", on_click) 
win.bind("", on_release) 
win.mainloop()

ผลลัพธ์

รันโค้ด จะแสดงหน้าต่างพร้อม label widget

วิธีตรวจจับการปล่อยปุ่มเมาส์ (Button Release) ใน Tkinter

คลิกที่ใดก็ได้ในหน้าต่าง เพื่อดูข้อความบนหน้าจอที่จะอัปเดตเมื่อปล่อยปุ่มเมาส์

วิธีตรวจจับการปล่อยปุ่มเมาส์ (Button Release) ใน Tkinter

เมื่อปล่อยปุ่มเมาส์ จะแสดงข้อความ:

วิธีตรวจจับการปล่อยปุ่มเมาส์ (Button Release) ใน Tkinter