如何解决当 frame.place() 位于单独的行中时,tkinter 子小部件消失在框架后面
当我定义以下函数时,我在函数内定义的所有其他小部件都会出现并且可以正常工作。我可以修改条目并从中获取值。
`
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,bg="white"
)
new_frm.place(x=self.x0,y=0)
new_frm = tk.Frame(self.window,bg=color)
self.frame_list.append(new_frm)
lbl = tk.Label(master=new_frm,y = 0)
self.x0 += 205
`
我想将 new_frm 放入 frame_list
以便我以后可以访问所有帧。当然,当我使用 new_frm = tk.Frame(self.window,bg="white").place(x=self.x0,y=0)
,new_frm
成为 nonetype 并且它不起作用。
更新:
@Kartikeya 提到的解决方案是对框架内的小部件使用 .grid()
。因此,代码必须这样写:
`
def frame(self):
new_frm = tk.Frame(self.window,20)
)
lbl.grid(row = 0,column = 0)
self.x0 += 205
`
解决方法
这是因为您在第二个/第三个示例中创建了两次 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,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,y=0)
# below line should not be called
#new_frm = tk.Frame(self.window,bg=color)
self.frame_list.append(new_frm)
lbl = tk.Label(master=new_frm,y = 0)
self.x0 += 205
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。