สมมติว่าเราต้องการเรียกใช้ฟังก์ชันเมื่อใดก็ตามที่มีการกดปุ่มหรือแป้นสำหรับแอปพลิเคชันเฉพาะ เราสามารถผูกฟังก์ชันที่มีการดำเนินการด้วยปุ่มหรือคีย์โดยใช้ bind(' ) กระบวนการ. ที่นี่ คุณสามารถผูกคีย์ใดก็ได้กับเหตุการณ์หรือฟังก์ชันที่ต้องการเรียกใช้
ตัวอย่าง
ในตัวอย่างนี้ เราได้สร้างฟังก์ชันที่จะเปิดกล่องโต้ตอบทุกครั้งที่เราคลิกปุ่ม
#Import the required libraries
from tkinter import *
from tkinter import ttk
from tkinter import messagebox
#Create an instance of Tkinter Frame
win = Tk()
#Set the geometry of Tkinter Frame
win.geometry("700x350")
#Define a function for opening the Dialog box
def open_prompt():
messagebox.showinfo("Message", "Click Okay to Proceed")
#Create a Label widget
Label(win, text= "Click to Open the MessageBox").pack(pady=15)
#Create a Button for opening a dialog Box
ttk.Button(win, text= "Open", command= open_prompt).pack()
win.mainloop() ผลลัพธ์
การเรียกใช้โค้ดด้านบนจะแสดงหน้าต่างที่มีป้ายกำกับและปุ่ม

เมื่อคลิกปุ่ม "เปิด" ระบบจะเรียกใช้ฟังก์ชันเพื่อเปิดกล่องโต้ตอบ
