简体   繁体   English

如何获取另一个 Toplevel 的 Toplevel 窗口的名称?

[英]How get name of Toplevel window of another Toplevel?

import tkinter as tk


r = tk.Tk()
r.title("root")
r.geometry("200x200")
f =tk.Frame(r)
f.pack()

top1 = tk.Toplevel(f)
top1.title("A")
#some widgets inside top2

top2 = tk.Toplevel(top1)
top2.title("B")  

print(top1.winfo_toplevel(), top1.winfo_children())

r.mainloop()

I only want to get the toplevel of top1 and nothing else ie .!frame.!toplevel.!toplevel我只想得到top1的顶层,没有别的,即.!frame.!toplevel.!toplevel

winfo_toplevel() returns .!frame.!toplevel winfo_toplevel()返回.!frame.!toplevel

winfo_children() returns .!frame.!toplevel.!toplevel , ... all children winfo_children()返回.!frame.!toplevel.!toplevel , ... 所有孩子

Your question is a bit unclear, but what I think you're asking is how to get top2 given only top1 .你的问题是有点不清楚,但我你问的是如何让top2只给出top1

There's no direct way to do that.没有直接的方法可以做到这一点。 You can get all of the children of top1 and then return any children that are Toplevel widgets.您可以获取top1所有子top1 ,然后返回作为Toplevel小部件的所有子项。 If you know that top1 only has a single child that is a Toplevel , you can then return the first child that is a Toplevel .如果您知道top1只有一个Toplevel ,那么您可以返回第一个Toplevel

def get_toplevel(w):
    for child in w.winfo_children():
        if child.winfo_class() == "Toplevel":
            return child
    raise Exception("No toplevel window was found")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM