获取 Tkinter 上的 Toplevel 列表
Get list of Toplevels on Tkinter
我想知道是否有一种简单的方法可以从特定 window 获取所有顶层,包括顶层中的顶层。在下面的代码中,我留下了一个我想做的例子:
from tkinter import Tk, Toplevel
v = Tk()
v2 = Toplevel(v)
v3 = Toplevel(v2)
v4 = Toplevel(v2)
def toplevels(ventana):
print("Here I return the list of all toplevels, in case of choosing the main window, it should return:")
print()
print(".")
print(".toplevel")
print(".toplevel.toplevel")
print(".toplevel.toplevel2")
toplevels(v)
Tkinter 是否内置了一些东西来完成这个?
每个小部件都有其子项的列表,使用递归您可以获得所有小部件。
from tkinter import Tk, Toplevel, Label
v = Tk()
v2 = Toplevel(v)
v3 = Toplevel(v2)
v4 = Toplevel(v2)
Label(v)
Label(v2)
Label(v3)
Label(v4)
def toplevels(ventana):
for k, v in ventana.children.items():
if isinstance(v, Toplevel):
print('Toplevel:', k, v)
else:
print(' other:', k, v)
toplevels(v)
toplevels(v)
结果
Toplevel: !toplevel .!toplevel
Toplevel: !toplevel .!toplevel.!toplevel
other: !label .!toplevel.!toplevel.!label
Toplevel: !toplevel2 .!toplevel.!toplevel2
other: !label .!toplevel.!toplevel2.!label
other: !label .!toplevel.!label
other: !label .!label
我想知道是否有一种简单的方法可以从特定 window 获取所有顶层,包括顶层中的顶层。在下面的代码中,我留下了一个我想做的例子:
from tkinter import Tk, Toplevel
v = Tk()
v2 = Toplevel(v)
v3 = Toplevel(v2)
v4 = Toplevel(v2)
def toplevels(ventana):
print("Here I return the list of all toplevels, in case of choosing the main window, it should return:")
print()
print(".")
print(".toplevel")
print(".toplevel.toplevel")
print(".toplevel.toplevel2")
toplevels(v)
Tkinter 是否内置了一些东西来完成这个?
每个小部件都有其子项的列表,使用递归您可以获得所有小部件。
from tkinter import Tk, Toplevel, Label
v = Tk()
v2 = Toplevel(v)
v3 = Toplevel(v2)
v4 = Toplevel(v2)
Label(v)
Label(v2)
Label(v3)
Label(v4)
def toplevels(ventana):
for k, v in ventana.children.items():
if isinstance(v, Toplevel):
print('Toplevel:', k, v)
else:
print(' other:', k, v)
toplevels(v)
toplevels(v)
结果
Toplevel: !toplevel .!toplevel
Toplevel: !toplevel .!toplevel.!toplevel
other: !label .!toplevel.!toplevel.!label
Toplevel: !toplevel2 .!toplevel.!toplevel2
other: !label .!toplevel.!toplevel2.!label
other: !label .!toplevel.!label
other: !label .!label