Tkinter เป็นไลบรารีมาตรฐานของ Python ที่ใช้สร้างและพัฒนาแอปพลิเคชัน berbasis GUI เราสามารถสร้างแอปพลิเคชันใน Tkinter และเพิ่มวิดเจ็ตเพื่อทำให้แอปพลิเคชันมีปฏิสัมพันธ์มากขึ้น
สมมติว่าเราต้องการแสดงกล่องโต้ตอบ (popup dialog) ในแอปพลิเคชัน ในกรณีนี้ เราสามารถใช้โมดูล messagebox ที่มาพร้อมกับ tkinter ได้ โมดูลนี้ช่วยให้เราแสดงกล่องโต้ตอบหลากหลายประเภท เช่น กล่องข้อผิดพลาด กล่องข้อมูล กล่องยืนยัน เป็นต้น
ตัวอย่าง
ในตัวอย่างนี้ เราสร้างปุ่มที่เมื่อคลิกจะแสดงข้อความ popup บนหน้าจอ
# Import the required library
from tkinter import *
from tkinter import ttk
from tkinter import messagebox
# Create an instance of tkinter frame
win = Tk()
# Set the geometry
win.geometry("700x250")
# Define a button to show the popup message box
def on_click():
messagebox.showinfo("Message", "Hey folks!")
# Add a Label widget
Label(win, text="Click the button to open a popup", font=('Georgia 13'))
# Create a button to open the popup dialog
ttk.Button(win, text="Open Popup", command=on_click).pack(pady=30)
win.mainloop()
ผลลัพธ์
การรันโค้ดด้านบนจะแสดงหน้าต่างพร้อมปุ่มสำหรับเปิดกล่องโต้ตอบ

คลิกปุ่มเพื่อแสดงกล่องโต้ตอบ popup บนหน้าจอ
