如何在 Tkinter 中将顶级窗口置于主窗口前?


Tkinter 顶级窗口会在主窗口之外创建一个新窗口。我们可以在新创建的顶级窗口中添加小组件和组件。它支持父窗口或主窗口的所有属性。

有时,顶级窗口也称为子窗口。若要将子窗口放在父窗口的前面,我们可以使用 wm_transient() 方法。

示例

# Import the required libraries
from tkinter import *
from tkinter import ttk

# Create an instance of tkinter frame or window
win=Tk()

# Set the size of the window
win.geometry("700x350")
win.title("Parent Window")

# Create a Toplevel window
top=Toplevel(win)
top.geometry('600x250')
top.title("Child Window")

# Place the toplevel window at the top
top.wm_transient(win)

win.mainloop()

输出

如果我们运行上述代码,它将在主窗口前显示一个顶级窗口。

更新于:2021 年 8 月 5 日

3K+ 视图

开启你的事业

完成课程后获得认证

开始
广告
© . All rights reserved.