Tkinter:将一个小部件放在一个框架中实际上是将它放在根中



所以我在Windows(python 3.8.5(和Linux(python 3.8.2(上遇到了一个独特的问题。当我把一个小部件放在某个框架中时,无论出于什么原因,它实际上都会放在根中。

我正在使用TkinterExtensions库。我看过这个答案,但没有帮助。无论我做什么,我都会遇到问题,但只针对一帧。在这一点上,我怀疑这可能是一个Tkinter库错误。

编辑:这似乎只发生在python 3.8.x中。我在另一台3.7.x的电脑上尝试过,结果不出所料。

下面是一个非常通用的例子。

from TkinterExtensions import *
class Root(tk.Tk):
# sets up Tkinter and creates the other windows and places them accordingly.
def __init__(self):
super().__init__()
self.w1 = Window1(root=self).place(relx=0.0, rely=0.0, relheight=1.0, relwidth=1.0)
self.w2 = Window2(root=self).place(relx=0.0, rely=0.0, relheight=1.0, relwidth=1.0)
self.w3 = Window3(root=self).place(relx=0.0, rely=0.0, relheight=1.0, relwidth=1.0)

self.w1.hide()
self.w2.hide()
self.w3.hide()
class Window1(TkinterFrame):
# ... anything needed for this frame
# This is the frame that's causing the issue.
# Despite the same code on all three windows, 
# this one puts any widgets into root instead of as a child of this frame.

def __init__(self, root)
super().__init__(root)
self.button = TkinterButton(master=self, Text="button").place(relx=0.0, rely=0.0, relheight=1.0, relwidth=1.0)

class Window2(TkinterFrame):
# ... anything needed for this frame
# this frame works fine

class Window3(TkinterFrame):
# ... anything needed for this frame
# this frame works fine

如果需要,我可以使用更广泛的通用代码创建一个存储库。

此外,我还是TkinterExtensions库的作者。

在这个特定的框架中,我定义了len方法。一旦a重命名了它,布局管理器和放置在其中的任何小部件都能按预期工作。Tkinter。Frame类没有定义len,所以我不知道它为什么会引起问题,但它确实引起了问题。

相关内容

最新更新