สมมติว่าเราต้องการสร้างกล่องโต้ตอบโดยใช้ tkinter ในการสร้างกล่องโต้ตอบ เราสามารถใช้ไลบรารี MessageBox ซึ่งมีฟังก์ชันหลายอย่างเพื่อสร้างประเภทการสนทนาได้อย่างรวดเร็ว
ในการปรับตำแหน่งของกล่องโต้ตอบที่สร้างขึ้น เราสามารถใช้คุณสมบัติ "ระดับบนสุด" ซึ่งโดยพื้นฐานแล้วจะให้ความสำคัญกับกล่องปัจจุบันและเก็บกระบวนการอื่นๆ ไว้ในแบ็กเอนด์
ประกอบด้วยฟังก์ชันอื่นๆ เช่น ชื่อ ข้อความ และรายละเอียด ในการเปลี่ยนตำแหน่งของวิดเจ็ต MessageBox เราจะใช้ เรขาคณิต วิธีการ
ตัวอย่าง
#import the tkinter library
from tkinter import *
#define the messagebox function
def messagebox():
#toplevel function creates MessageBox dialog which appears on top of the screen
top=Toplevel(win)
top.title("Click Me")
#Define the position of the MessageBox
x_position = 600
y_position = 400
top.geometry(f"600x200+{x_position}+{y_position}")
#Define the property of the messageBox
l1=Label(top, text= "Hello! TutorialsPoint",bg= "green", fg=
"white",font=('Times New Roman', 24),height=50, width= 50).pack()
#Create an instance of the tkinter frame
#And resize the frame
win = Tk()
win.geometry("600x200")
win.title("Window-1")
Button(win, text="Click Me", command=messagebox,
width=8).pack(pady=80)
win.mainloop() ผลลัพธ์
การเรียกใช้โค้ดด้านบนจะสร้างหน้าต่างเอาต์พุตต่อไปนี้

เมื่อคุณคลิกปุ่ม “คลิกฉัน” กล่องโต้ตอบจะเปิดขึ้นซึ่งสามารถวางตำแหน่งได้ในภายหลัง
