Giả sử muốn gọi hàm khi nhấn nút hoặc phím. Có thể gắn hàm với nút/phím bằng phương thức bind('
Ví dụ
Tạo hàm mở hộp thoại khi nhấn nút.
# Import thư viện
from tkinter import *
from tkinter import ttk
from tkinter import messagebox
# Tạo khung Tkinter
win = Tk()
win.geometry("700x350")
# Hàm mở hộp thoại
def open_prompt():
messagebox.showinfo("Message", "Click Okay to Proceed")
# Tạo Label
Label(win, text="Click to Open the MessageBox").pack(pady=15)
# Tạo nút mở hộp thoại
ttk.Button(win, text="Open", command=open_prompt).pack()
win.mainloop()Kết quả
Hiển thị cửa sổ có nhãn và nút.

Nhấn "Open" sẽ gọi hàm mở hộp thoại.
