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()
我只想獲得頂級,top1沒有別的,即.!frame.!toplevel.!toplevel
winfo_toplevel() 回傳 .!frame.!toplevel
winfo_children()回傳.!frame.!toplevel.!toplevel, ... 所有孩子
uj5u.com熱心網友回復:
你的問題有點不清楚,但我認為你問的是如何top2只得到給定top1。
沒有直接的方法可以做到這一點。您可以獲取所有子項top1,然后回傳作為Toplevel小部件的所有子項。如果您知道top1只有一個孩子是 a Toplevel,那么您可以回傳第一個孩子是 a 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")
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/340146.html
上一篇:tkinter函式如何處理它作為引數的串列,一一,一一?
下一篇:更改物件宣告Python
