ภาพรวม
สมมติทำงานกับ Tkinter App มี Buttons ที่ต้อง Pull Out Window หรือ Event เพื่อทำให้ Button ทำงานเต็มที่ ส่ง Arguments บางอย่างเป็น Command Value
Command เป็น Button Attribute รับ Function Name เป็น Value Function นิยามการทำงานของ Event นั้น สร้าง Button และเพิ่ม Events โดยส่ง Arguments ให้ Command Attribute
ตัวอย่างโค้ด
# 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()
ผลลัพธ์
รันโค้ดจะส่ง Function เป็น Argument ให้ Button Command

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