当 frame.place() 位于单独的行中时,tkinter 子小部件会消失在框架后面
tkinter child widgets disappear behind the frame when frame.place() is position in a separate line
当我定义以下函数时,我在该函数内定义的所有其他小部件都会出现并且可以正常工作。我可以修改条目并从中获取值。
`
def frame(self):
new_frm = tk.Frame(self.window, width=200,
height=200, bg="white"
).place(x=self.x0, y=0)
self.frame_list.append(new_frm)
lbl = tk.Label(master=new_frm, relief=tk.RIDGE,
text=param_name, fg = "white", bg="black",
anchor="w", font=("Arial", 20)
)
lbl.place(x = self.x0, y = 0)
self.x0 += 205
`
但是如果我将代码更改为下面的代码,则只会出现框架而不是小部件。但是,这些小部件仍然与第一种情况一样有效。我想知道为什么会发生这种情况以及如何解决它。
`
def frame(self):
new_frm = tk.Frame(self.window,
width=200, height=200, bg="white"
)
new_frm.place(x=self.x0, y=0)
new_frm = tk.Frame(self.window, width=200, height=200, bg=color)
self.frame_list.append(new_frm)
lbl = tk.Label(master=new_frm, relief=tk.RIDGE,
text=param_name, fg = "white", bg="black",
anchor="w", font=("Arial", 20)
)
lbl.place(x = self.x0, y = 0)
self.x0 += 205
`
我想将 new_frm 放入 frame_list
以便我以后可以访问所有帧。当然,当我使用 new_frm = tk.Frame(self.window, width=200,height=200, bg="white").place(x=self.x0, y=0)
, new_frm
变成 Nonetype 并且不起作用。
更新:
@Kartikeya 提到的解决方案是对框架内的小部件使用 .grid()
。因此,代码必须这样写:
`
def frame(self):
new_frm = tk.Frame(self.window,
width=200, height=200, bg="white"
)
new_frm.place(x=self.x0, y=0)
new_frm = tk.Frame(self.window, width=200, height=200, bg=color)
self.frame_list.append(new_frm)
lbl = tk.Label(master=new_frm, relief=tk.RIDGE,
text=param_name, fg = "white", bg="black",
anchor="w", font=("Arial", 20)
)
lbl.grid(row = 0, column = 0)
self.x0 += 205
`
这是因为您在second/third中创建了两次new_frm
例如:
def frame(self):
# first "new_frm"
new_frm = tk.Frame(self.window,
width=200, height=200, bg="white"
)
new_frm.place(x=self.x0, y=0)
# second "new_frm" which ovrrides the above one
# and it is invisible because it is not .pack()/grid()/place()
new_frm = tk.Frame(self.window, width=200, height=200, bg=color)
self.frame_list.append(new_frm)
# label are put in the invisible "new_frm", so it is not visible as well
lbl = tk.Label(master=new_frm, relief=tk.RIDGE,
text=param_name, fg = "white", bg="black",
anchor="w", font=("Arial", 20)
)
lbl.place(x = self.x0, y = 0)
self.x0 += 205
移除new_frm
的二次创作:
def frame(self):
new_frm = tk.Frame(self.window,
width=200, height=200, bg="white"
)
new_frm.place(x=self.x0, y=0)
# below line should not be called
#new_frm = tk.Frame(self.window, width=200, height=200, bg=color)
self.frame_list.append(new_frm)
lbl = tk.Label(master=new_frm, relief=tk.RIDGE,
text=param_name, fg = "white", bg="black",
anchor="w", font=("Arial", 20)
)
lbl.place(x = self.x0, y = 0)
self.x0 += 205
当我定义以下函数时,我在该函数内定义的所有其他小部件都会出现并且可以正常工作。我可以修改条目并从中获取值。
`
def frame(self):
new_frm = tk.Frame(self.window, width=200,
height=200, bg="white"
).place(x=self.x0, y=0)
self.frame_list.append(new_frm)
lbl = tk.Label(master=new_frm, relief=tk.RIDGE,
text=param_name, fg = "white", bg="black",
anchor="w", font=("Arial", 20)
)
lbl.place(x = self.x0, y = 0)
self.x0 += 205
`
但是如果我将代码更改为下面的代码,则只会出现框架而不是小部件。但是,这些小部件仍然与第一种情况一样有效。我想知道为什么会发生这种情况以及如何解决它。
`
def frame(self):
new_frm = tk.Frame(self.window,
width=200, height=200, bg="white"
)
new_frm.place(x=self.x0, y=0)
new_frm = tk.Frame(self.window, width=200, height=200, bg=color)
self.frame_list.append(new_frm)
lbl = tk.Label(master=new_frm, relief=tk.RIDGE,
text=param_name, fg = "white", bg="black",
anchor="w", font=("Arial", 20)
)
lbl.place(x = self.x0, y = 0)
self.x0 += 205
`
我想将 new_frm 放入 frame_list
以便我以后可以访问所有帧。当然,当我使用 new_frm = tk.Frame(self.window, width=200,height=200, bg="white").place(x=self.x0, y=0)
, new_frm
变成 Nonetype 并且不起作用。
更新:
@Kartikeya 提到的解决方案是对框架内的小部件使用 .grid()
。因此,代码必须这样写:
`
def frame(self):
new_frm = tk.Frame(self.window,
width=200, height=200, bg="white"
)
new_frm.place(x=self.x0, y=0)
new_frm = tk.Frame(self.window, width=200, height=200, bg=color)
self.frame_list.append(new_frm)
lbl = tk.Label(master=new_frm, relief=tk.RIDGE,
text=param_name, fg = "white", bg="black",
anchor="w", font=("Arial", 20)
)
lbl.grid(row = 0, column = 0)
self.x0 += 205
`
这是因为您在second/third中创建了两次new_frm
例如:
def frame(self):
# first "new_frm"
new_frm = tk.Frame(self.window,
width=200, height=200, bg="white"
)
new_frm.place(x=self.x0, y=0)
# second "new_frm" which ovrrides the above one
# and it is invisible because it is not .pack()/grid()/place()
new_frm = tk.Frame(self.window, width=200, height=200, bg=color)
self.frame_list.append(new_frm)
# label are put in the invisible "new_frm", so it is not visible as well
lbl = tk.Label(master=new_frm, relief=tk.RIDGE,
text=param_name, fg = "white", bg="black",
anchor="w", font=("Arial", 20)
)
lbl.place(x = self.x0, y = 0)
self.x0 += 205
移除new_frm
的二次创作:
def frame(self):
new_frm = tk.Frame(self.window,
width=200, height=200, bg="white"
)
new_frm.place(x=self.x0, y=0)
# below line should not be called
#new_frm = tk.Frame(self.window, width=200, height=200, bg=color)
self.frame_list.append(new_frm)
lbl = tk.Label(master=new_frm, relief=tk.RIDGE,
text=param_name, fg = "white", bg="black",
anchor="w", font=("Arial", 20)
)
lbl.place(x = self.x0, y = 0)
self.x0 += 205