微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决Tkinter:将小部件放置在框架中实际上将其放置在根中

所以我有一个独特的问题,该问题发生在Windows(python 3.8.5)和Linux(python 3.8.2)上。当我将窗口小部件放置在某个框架中时,无论出于何种原因,它实际上都放置在Root中。

我正在使用TkinterExtensions库。我看过这个Answer,但没有帮助。无论我做什么,我都会遇到问题,但仅限于一帧。我怀疑此时可能是Tkinter库错误

编辑:它似乎仅在python 3.8.x中发生。我在另一台装有3.7.x的PC上尝试过,它按预期工作。

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

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,relwidth=1.0)
        self.w3 = Window3(root=self).place(relx=0.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,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 方法。重命名后,布局管理器以及其中放置的所有小部件都能按预期工作。 Tkinter.Frame类没有定义 len ,所以我不知道为什么它引起了问题,但是确实如此。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。