เมธอด invoke() ของ Button widget ใช้เรียก callback function โดยไม่ต้องมีการคลิกจริง เหมาะสำหรับการทริกเกอร์เหตุการณ์อัตโนมัติ
ตัวอย่างโค้ด
from tkinter import *
from tkinter import ttk
from tkinter import messagebox
win = Tk()
win.geometry("700x350")
def display_msg():
messagebox.showinfo("Message", "Hello There! Greeting from TutorialsPoint.")
b1 = ttk.Button(win, text="Click Me", command=display_msg)
b1.pack(pady=30)
# เรียก event ทันทีเมื่อโหลด
b1.invoke()
win.mainloop()
ผลลัพธ์
กล่องข้อความจะแสดงทันทีเมื่อโปรแกรมเริ่มรัน ก่อนที่ผู้ใช้จะคลิก
