คลาส toplevel ของ Tkinter มี toplevel window ซึ่งเป็น child window นอกเหนือจากหน้าต่างหลัก ทุกครั้งที่เราสร้าง toplevel window มันจะปรากฏเหนือหน้าต่างหลักพร้อมกับ widget ที่กำหนดในนั้น
เพื่อให้หน้าต่าง toplevel window อยู่ในโฟกัสเสมอ เราสามารถใช้เมธอด grab_set() มันจะเก็บ toplevel window ให้อยู่เหนือหน้าต่างอื่นๆ เสมอ
ตัวอย่าง
# นำเข้าไลบรารี tkinter
from tkinter import *
# สร้างอินสแตนซ์ของ tkinter frame
win = Tk()
win.geometry("700x350")
def open_win():
top = Toplevel(win)
top.geometry("700x250")
Label(top, text="Hey Folks!", font=('Helvetica 14 bold')).pack()
top.grab_set()
# สร้าง Label พิมพ์ชื่อ
label = Label(win, text="Click the below Button to open the Popup", font=('Helvetica 18 bold'))
label.pack(pady=30)
# สร้างปุ่ม
button = Button(win, text="Click Me", command=open_win, font=('Helvetica 14 bold'), foreground='OrangeRed3', background="white")
button.pack(pady=50)
win.mainloop()
ผลลัพธ์
การรันโค้ดจะแสดงหน้าต่างที่มีปุ่มเปิด Popup window

ตอนนี้ คลิกปุ่มเพื่อเปิด Popup window
