Python tkinter - 使用嵌套框架时无法定位子框架

Python tkinter - Unable to position child frame while using nested frames

我正在试验 tkinter,

#创建黑框

frame=Frame(root, width=366, height=559,bg="black")
frame.pack(padx=(0,0),pady=(0,0))

#Placed gray frame and magenta frame ON black frame

frame2=Frame(frame, width=349, height=57,bg="grey").place(x=8, y=10)
frame3=Frame(frame, width=70, height=473,bg="magenta").place(x=8, y=74)

#Placing Blue frame ON magenta frame, at (0,0), but this goes to the root's (0,0)

frame4=Frame(frame3, width=50, height=50,bg="blue").place(x=0, y=0)

灰色和品红色框相对于黑色框位置完美 但是蓝色框转到根 window 的 (0,0)。为什么?我如何将蓝色框放在洋红色框上?

完整代码:

from tkinter import *
root = Tk()
root.title("Fee Fie Foe Fum")
frame=Frame(root, width=366, height=559,bg="black")
frame.pack(padx=(0,0),pady=(0,0))
frame2=Frame(frame, width=349, height=57,bg="grey").place(x=8, y=10)
frame3=Frame(frame, width=70, height=473,bg="magenta").place(x=8, y=74)
frame4=Frame(frame3, width=50, height=50,bg="blue").place(x=0, y=0)
root.mainloop()

蓝色框 frame4 进入根 window 因为它使用 frame3 作为父项。 frame3None,tkinter 对待它的方式与传递根 window.

相同

之所以frame3None是因为place(以及packgrid)都returnNone .因此,Frame(...).place(...) returns None.

恕我直言,最好将小部件创建与小部件布局分开。这导致需要一些临时变量,但使用单一样式(始终将两者分开)使代码比有时使用一种样式有时使用另一种样式更易于管理。