为了账号安全,请及时绑定邮箱和手机立即绑定

Tkinter:将小部件放置在框架中实际上是将其放置在根目录中

Tkinter:将小部件放置在框架中实际上是将其放置在根目录中

翻阅古今 2023-09-05 20:28:16
所以我在 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库的作者。
查看完整描述

1 回答

?
人到中年有点甜

TA贡献1895条经验 获得超7个赞

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



查看完整回答
反对 回复 2023-09-05
  • 1 回答
  • 0 关注
  • 90 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信