【发布时间】:2020-10-12 16:39:50
【问题描述】:
我开始使用 Tkinter 应用程序处理类,但我似乎不了解类的工作原理,尤其是关系父控制器。正如您在下面的代码中看到的那样,我计划为整个部分创建一个外部类,然后为该框架内的每个部分创建 4 个内部类。但是,我不能从初始帧调用这些类。有没有更好的方法来做到这一点?我做错了什么?
class MainScreenFrameCenter(tk.Frame):
def __init__(self, parent, controller,*args,**kwargs):
tk.Frame.__init__(self,parent, bg="white",height=680, width=640,highlightbackground="black", highlightthickness=1)
self.controller = controller
self.pack(side="top", fill="both", expand=True)
self.widgets_nw = MainScreenFrameCenterNW(parent=self,controller=self)
self.widgets_sw = MainScreenFrameCenterSW(parent=self,controller=self)
self.widgets_ne = MainScreenFrameCenterNE(parent=self,controller=self)
self.widgets_se = MainScreenFrameCenterSE(parent=self,controller=self)
class MainScreenFrameCenterNW(tk.Frame):
def __init__(self, parent, controller,*args,**kwargs):
tk.Frame.__init__(self,parent,height=350,width=640,bg="white",highlightbackground="black",highlightthickness=1)
self.controller = controller
self.grid(row=0,column=0,sticky="nsew")
class MainScreenFrameCenterSW(tk.Frame):
def __init__(self, parent, controller,*args,**kwargs):
tk.Frame.__init__(self,parent,height=350,width=640,bg="white",highlightbackground="black",highlightthickness=1)
self.controller = controller
self.grid(row=1,column=0,sticky="nsew")
class MainScreenFrameCenterNE(tk.Frame):
def __init__(self, parent, controller,*args,**kwargs):
tk.Frame.__init__(self,parent,height=350,width=640,bg="white",highlightbackground="black",highlightthickness=1)
self.controller = controller
self.grid(row=0,column=1,sticky="nsew")
class MainScreenFrameCenterSE(tk.Frame):
def __init__(self, parent, controller,*args,**kwargs):
tk.Frame.__init__(self,parent,height=350,width=640,bg="white",highlightbackground="black",highlightthickness=1)
self.controller = controller
self.grid(row=1,column=1,sticky="nsew")
【问题讨论】:
-
“我无法从初始帧调用这些类。” - 为什么不呢? “调用这些类”是什么意思?当你尝试时会发生什么?
-
您是否有意将其他类放入
ManScreenFrameCenter?这很不寻常。 -
例如,它说 MainScreenFrameCenterNW 不存在。 FrameCenter 基本上是更大的框架,分为 4 个部分。我将它们设置在 MainScreenFrameCenter 中,因为它们属于该框架,我应该将它们放在外面吗?