ในการเรนเดอร์วิดเจ็ตในแอปพลิเคชัน Tkinter โดยทั่วไปเราใช้ mainloop() ฟังก์ชันที่ช่วยแสดงวิดเจ็ตในหน้าต่าง ในหลายกรณี หน้าต่าง tkinter จะแสดงเหนือหน้าต่างหรือโปรแกรมอื่นๆ ขณะเปลี่ยนไปใช้โปรแกรมหรือหน้าต่างอื่น การค้นหาและเปลี่ยนกลับไปใช้หน้าต่าง Tkinter นั้นทำได้ยาก
เราสามารถบังคับให้หน้าต่าง tkinter ของเราอยู่ด้านบนของหน้าต่างหรือโปรแกรมอื่น ๆ โดยการสร้างฟังก์ชันและกำหนด win.lift() วิธีการในวง ในลูปมันจะรัน win.after(2000, function()) เพื่อให้แน่ใจว่าหน้าต่าง tkinter จะอยู่ด้านบนของหน้าต่างอื่นเสมอ
ตัวอย่าง
# Import the required libraries
from tkinter import *
import lorem
# Create an instance of tkinter frame or window
win=Tk()
# Set the size of the window
win.geometry("700x350")
def stay_on_top():
win.lift()
win.after(2000, stay_on_top)
# Add a Label widget
Label(win, text="This window will always stay on Top", font=('Aerial 14')).pack(pady=30, anchor =CENTER)
# Call function to make the window stay on top
stay_on_top()
win.mainloop() ผลลัพธ์
การเรียกใช้โค้ดด้านบนจะแสดงหน้าต่างที่จะอยู่ด้านบนของหน้าต่างอื่นๆ ทั้งหมดโดยอัตโนมัติ
