สมมติว่าเรากำลังทำงานกับแอปพลิเคชัน tkinter ซึ่งมีปุ่มบางปุ่มที่ต้องดึงหน้าต่างหรือเหตุการณ์ออก เพื่อให้ปุ่มทำงานได้อย่างสมบูรณ์ เราสามารถส่งต่ออาร์กิวเมนต์เป็นค่าคำสั่งได้
คำสั่งเป็นแอตทริบิวต์ปุ่มซึ่งใช้ชื่อฟังก์ชันเป็นค่า ฟังก์ชันกำหนดการทำงานของเหตุการณ์เฉพาะ
ขั้นแรกให้เราสร้างปุ่มและเพิ่มเหตุการณ์บางอย่างโดยส่งผ่านอาร์กิวเมนต์ไปยังแอตทริบิวต์คำสั่ง
ตัวอย่าง
ในตัวอย่างนี้ เราจะสร้างหน้าต่างและปุ่มที่จะปิดหน้าต่างทันที
#Importing the required library
from tkinter import *
#Create an instance of tkinter frame or window
win= Tk()
#Set the title
win.title("Button Command Example")
#Set the geometry
win.geometry("600x300")
#Create a label for the window
Label(win, text= "Example", font= ('Times New Roman bold',
20)).pack(pady=20)
#Defining a function
def close_event():
win.destroy()
#Create a button and pass arguments in command as a function name
my_button= Button(win, text= "Close", font=('Helvetica bold', 20),
borderwidth=2, command= close_event)
my_button.pack(pady=20)
win.mainloop() ผลลัพธ์
โดยเรียกใช้โค้ดด้านบนนี้ เราสามารถส่งฟังก์ชันเป็นอาร์กิวเมนต์ไปยังคำสั่ง Button ได้

คลิกปุ่ม "ปิด" และหน้าต่างจะปิดลง