Tkinter Events มีประโยชน์มากสำหรับแอปพลิเคชันใด ๆ ที่เราจำเป็นต้องทำงานหรือดำเนินการบางอย่าง ใน Tkinter โดยทั่วไปเหตุการณ์จะถูกสร้างขึ้นโดยการกำหนดฟังก์ชันซึ่งมีส่วนของโค้ดและตรรกะสำหรับเหตุการณ์บางอย่าง ในการเรียกเหตุการณ์ โดยทั่วไปเราจะผูกเหตุการณ์ด้วยคีย์หรือวิดเจ็ตปุ่ม ฟังก์ชันผูกใช้พารามิเตอร์สองตัว ('
โดยใช้วิธีการเดียวกันในตัวอย่างต่อไปนี้ เราจะเรียกข้อความป๊อปอัปโดยการกดคีย์ผสม
ตัวอย่าง
# Import the required libraries
from tkinter import *
from tkinter import messagebox
# Create an instance of tkinter frame
win= Tk()
# Set the size of the tkinter window
win.geometry("700x350")
# Define a function to show the popup message
def show_msg(e):
messagebox.showinfo("Message","Hey There! I hope you are doing well.")
# Add an optional Label widget
Label(win, text = "Admin Has Sent You a Message. " "Press <Shift+Tab> to View the Message.", font = ('Aerial 15')).pack(pady= 40)
# Bind the Shift+Tab key with the event
win.bind('<Shift-Tab>', lambda e: show_msg(e))
win.mainloop() ผลลัพธ์
เมื่อเรารันโปรแกรมข้างต้น จะแสดงหน้าต่างที่มีวิดเจ็ตป้ายกำกับ เมื่อเรากดคีย์ผสม
