【问题标题】:tkinter child widgets disappear behind the frame when frame.place() is position in a separate line当 frame.place() 位于单独的行中时,tkinter 子小部件消失在框架后面
【发布时间】:2021-09-11 07:16:31
【问题描述】:

当我定义以下函数时,我在函数中定义的所有其他小部件都会出现并且可以正常工作。我可以修改条目并从中获取值。

`

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

`

【问题讨论】:

  • 如何创建其他小部件?请注意,new_frm 在第一个示例中为 None。
  • 如果您将.pack().grid() 与这些子小部件一起使用,则会出现此问题,因为您在第二种情况下将.place() 与框架一起使用。你可以从this answer得到一个想法
  • 对于你的情况,一个快速修复可以是对框架的所有子小部件做place(),你的问题就会得到解决
  • @acw1668 是的。这就是为什么我需要将它放在单独的一行中,这样我就可以只制作一个框架列表。
  • @Kartikeya 够公平的。父母涵盖所有孩子是完全有道理的。我应该想到的。但是,这仍然是一个谜,这仅发生在函数内部。当我将其从功能中取出时,框架不会覆盖子小部件。我没有完全得到你建议的快速修复。你能澄清一下吗?

标签: python tkinter widget


【解决方案1】:

这是因为您在第二个/第三个示例中创建了两次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

【讨论】:

  • 感谢@acw1668。这是我的一个愚蠢的错误。
猜你喜欢
  • 2011-04-27
  • 2020-01-14
  • 2012-08-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-09-04
  • 1970-01-01
相关资源
最近更新 更多