【发布时间】: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