【问题标题】:How can I integrate my classes in a final class?如何将我的课程整合到最终课程中?
【发布时间】:2019-04-11 20:20:18
【问题描述】:

在另一个关于 Python 代码结构的问题中,提出了一种解决方案: 问题在这里可以找到:Best way to structure a tkinter application

class Navbar(tk.Frame): ...
class Toolbar(tk.Frame): ...
class Statusbar(tk.Frame): ...
class Main(tk.Frame): ...

class MainApplication(tk.Frame):
    def __init__(self, parent, *args, **kwargs):
        tk.Frame.__init__(self, parent, *args, **kwargs)
        self.statusbar = Statusbar(self, ...)
        self.toolbar = Toolbar(self, ...)
        self.navbar = Navbar(self, ...)
        self.main = Main(self, ...)

        self.statusbar.pack(side="bottom", fill="x")
        self.toolbar.pack(side="top", fill="x")
        self.navbar.pack(side="left", fill="y")
        self.main.pack(side="right", fill="both", expand=True)

我喜欢这个解决方案,并在将它应用到我的代码之前尝试小规模地复制它。 谁能帮我设置应用程序缺少哪些参数、参数? 请参阅下面的代码:

import tkinter as tk


class Main(tk.Frame):

    def __init__(self, master):
        central = tk.Frame(master)
        central.pack(side="top", fill="both")

class SubMain(tk.Frame):

    def __init__(self,master):
        lowercentral = tk.Frame(master)
        lowercentral.pack(side="top", fill="both")

class MainApplication(tk.Frame):

    def __init__(self, parent):
        tk.Frame.__init__(self, parent)
        self.central = Main(self)
        self.lowercentral = SubMain(self)

        self.central.pack(side="top", fill="both")
        self.lowercentral.pack(side="top", fill="both")


root = tk.Tk()
MainApplication(root).pack(side="top", fill="both")
root.mainloop()

我的代码几句话。我希望代码基本上只是打开一个空的白色窗口。 Main 和 SubMain 类应该创建两个框架。 MainApplication 应该集成这两个类并有效地充当所有类的中心。

但是,我收到错误消息:

AttributeError: 'Main' 对象没有属性 'tk'

我假设,在我的示例中,我在 MainApplication 的 init 函数中缺少参数,但我的变体没有产生任何成功。

有人可以帮我解决这个问题吗?

【问题讨论】:

    标签: python python-3.x oop tkinter


    【解决方案1】:

    首先,当您实例化MainSubMain 类时,您需要传递parent 而不是MainApplication 实例(自身)。 然后你不需要在类上调用pack 方法,因为MainSubMain 类已经打包了它们的框架:

    import tkinter as tk
    
    class Main(tk.Frame):
    
        def __init__(self, master):
            central = tk.Frame(master)
            central.pack(side="top", fill="both")
    
    class SubMain(tk.Frame):
    
        def __init__(self,master):
            lowercentral = tk.Frame(master)
            lowercentral.pack(side="top", fill="both")
    
    class MainApplication(tk.Frame):
    
        def __init__(self, parent):
            tk.Frame.__init__(self, parent)
            self.central = Main(parent)
            self.lowercentral = SubMain(parent)
    
            #self.central.pack(side="top", fill="both")
            #self.lowercentral.pack(side="top", fill="both")
    
    
    root = tk.Tk()
    MainApplication(root)#.pack(side="top", fill="both")
    root.mainloop()
    

    【讨论】:

    • 太棒了!非常感谢您的回答。然而,也可以调用 super().__init__()。您的解决方案也可以,但不使用 super()。做什么比较好? (我更喜欢您的解决方案,因为它为我节省了 pack()。)我认为 super() 用于继承的情况,因此认为它不适用于这种情况。
    【解决方案2】:

    你可以在 init 函数中试试这个:

     super().__init__(master)
    

    我使用它并且它有效。希望它有效。

    【讨论】:

      【解决方案3】:

      确保在您的所有__init__ 函数中调用super().__init__()MainSubMain 中缺少它们。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-05-17
        • 2018-08-03
        • 1970-01-01
        • 1970-01-01
        • 2014-09-18
        • 2017-10-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多