ภาพรวม
สมมติต้องการสร้างแอปพลิเคชันที่เพิ่มคำอธิบายบน Widget Tkinter เช่น แสดง Tooltip Text เมื่อ Hover บน Button Widget สามารถทำได้โดยเพิ่ม Tooltip หรือ Popup
Tooltip มีประโยชน์ในแอปที่ต้องการ User Interaction สามารถนิยาม Tooltip โดยสร้าง Constructor ของ Balloon(win) จากนั้น Bind Button กับ Tooltip Message ที่ใช้กับ Widget
ตัวอย่างโค้ด
# Import the tkinter library
from tkinter import *
from tkinter.tix import *
# Create an instance of tkinter frame
win = Tk()
# Set the geometry
win.geometry("400x200")
# Create a tooltip
tip = Balloon(win)
# Create a Button widget
my_button = Button(win, text="Python", font=('Helvetica bold', 20))
my_button.pack(pady=20)
# Bind the tooltip with button
tip.bind_widget(my_button, balloonmsg="Python is an interpreted, high-level and general-purpose programming language")
win.mainloop()
ผลลัพธ์
รันโค้ดจะแสดงหน้าต่างพร้อม Button ตอน Hover ที่ Button "Python" จะแสดง Tooltip Text
