Tkinter มี events มากมายสำหรับโต้ตอบกับผู้ใช้ รายการทั่วไป:
| Event | คำอธิบาย |
|---|---|
<Button> | คลิกเมาส์ (Button-1=ซ้าย, Button-2=กลาง, Button-3=ขวา) |
<ButtonRelease> | ปล่อยปุ่มเมาส์ |
<Configure> | widget เปลี่ยนขนาด/ตำแหน่ง |
<Destroy> | widget ถูกทำลาย |
<Enter> | เมาส์เข้า widget |
<FocusIn> | widget ได้ focus |
<FocusOut> | widget สูญเสีย focus |
<KeyPress> | กดปุ่มคีย์บอร์ด |
<KeyRelease> | ปล่อยปุ่มคีย์บอร์ด |
<Leave> | เมาส์ออกจาก widget |
<Motion> | เมาส์เคลื่อนที่ |
<Visibility> | widget มองเห็นได้ |
ตัวอย่าง: Bind Button-1
from tkinter import *
win = Tk()
win.geometry("700x350")
def display_text(e):
label.config(text="Code never lies, comments sometimes do", font=('Helvetica 17 bold'))
label = Label(win, text="")
label.pack(pady=50)
win.bind('', display_text)
win.mainloop()
ผลลัพธ์
คลิกซ้ายที่หน้าต่าง → แสดงข้อความ
