Parent/master 与 tkinter 中的 in_

Parent/master vs. in_ in tkinter

检查以下代码:

import Tkinter as tk

root=tk.Tk()
f1 = tk.Frame(width=200, height=200, background="red")
f2 = tk.Frame(width=100, height=100, background="blue")

f1.pack(fill="both", expand=True, padx=20, pady=20)
f2.place(in_=f1, anchor="c", relx=.5, rely=.5)

root.mainloop()

取自:How do I center a frame within a frame in Tkinter?

其中一条评论说 "The root window is the default when a widget doesn't explicitly specify a parent" 还有 " In this case f1 is being managed by the root window and f2 is being managed by f1 (because of the in_ parameter)."

新小部件实例化中使用的 master 参数创建的层次结构与 .place 布局管理器中使用的 in_ 参数创建的层次结构有什么区别?

为什么不将 f2 创建为 f1 的子级? (它会改变什么吗?)

我的不理解似乎是我的代码出现问题的结果,我在这里得到了答案:

摆脱那里描述的问题我现在可以看到:

Master/parent:定义限制其子部件

呈现的边界区域

in_: 设置 in_(cluded) 小部件的 positioning 所基于的小部件

What is the difference between the hierarchy created by the master parameter used in the instantiation of new widgets and the hierarchy created by the the in_ prameter used in the .place layout manager?

简单的回答是 in_ 参数没有创建层次结构。唯一的层次结构是创建小部件时的 parent/child 关系。

in_ 参数仅标识应将另一个小部件放入其中的小部件。它告诉 tkinter "make this child's placement relative to this other widget".

Why is f2 not being created as a child of f1? (and would it change anything?)

它不是作为子项创建的,因为您没有告诉它是子项。您没有指定父级,因此 tkinter 使用根 window 作为父级。

如果你这样做,它会改变什么吗?不是在这个特定的情况下。如果 f1 没有填满整个 window,或者不对称地填满,f2 会出现在 f1 的中心,而不是整个 window 的中心。