猿问

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库的作者。


翻阅古今
浏览 91回答 1
1回答

人到中年有点甜

在这个特定的框架中,我定义了len方法。一旦重命名,布局管理器以及放置在其中的任何小部件就会按预期工作。Tkinter.Frame 类没有定义len所以我不知道为什么它会导致问题,但它确实发生了。
随时随地看视频慕课网APP

相关分类

Python
我要回答