โดยทั่วไปแล้ว Tkinter Events จะใช้เพื่อให้อินเทอร์เฟซที่ทำงานเป็นสะพานเชื่อมระหว่างผู้ใช้และตรรกะของแอปพลิเคชัน เราสามารถใช้กิจกรรมในแอปพลิเคชัน Tkinter เพื่อให้มีการโต้ตอบและใช้งานได้มากขึ้น กิจกรรม เช่น
ตัวอย่าง
ในตัวอย่างนี้ เราจะสร้างสคริปต์ที่จะแสดงข้อความบนหน้าจอทุกครั้งที่เรากดปุ่ม ข้อความจะหายไปเมื่อเราปล่อยคีย์เดียวกัน
# 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 key_press(e):
label.config(text="Welcome to TutorialsPoint")
def key_released(e):
label.config(text="Press any Key...")
# Create a label widget to add some text
label= Label(win, text= "", font= ('Helvetica 17 bold'))
label.pack(pady= 50)
# Bind the Mouse button event
win.bind('<KeyPress>',key_press)
win.bind('<KeyRelease>',key_released )
win.mainloop() ผลลัพธ์
การเรียกใช้โค้ดด้านบนจะแสดงหน้าต่างที่มีป้ายกำกับ

เมื่อคุณกดแป้นจากแป้นพิมพ์ จะแสดงข้อความบนหน้าจอ ในขณะเดียวกัน ข้อความจะได้รับการอัปเดตทุกครั้งที่คุณถอดกุญแจ
