【问题标题】:Problem with Tkinter - Inheritance problemsTkinter 的问题 - 继承问题
【发布时间】: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 中,因为它们属于该框架,我应该将它们放在外面吗?

标签: python class tkinter


【解决方案1】:

您需要将所有类定义移动到全局范围,方法是将它们全部放在相同的缩进级别。

class MainScreenFrameCenter(tk.Frame):
    ...
class MainScreenFrameCenterNW(tk.Frame):
    ...
class MainScreenFrameCenterSW(tk.Frame):
    ...
class MainScreenFrameCenterNE(tk.Frame):
    ...
class MainScreenFrameCenterSE(tk.Frame):
    ...

【讨论】:

    【解决方案2】:

    您似乎正在尝试制作一个小网格。类通常不嵌套在另一个类中。如果您创建一个代表 1 个网格单元的类,则可以使用循环从中创建一个网格。

    import tkinter as tk
    
    
    class Cell(tk.Frame):
        def __init__(self, master, column:int=0, row:int=0, **kwargs):
            kwargs = {**{'bg':'white', 'highlightbackground':'black','highlightthickness':1}, **kwargs}
            tk.Frame.__init__(self, master, **kwargs)
            self.grid(column=column, row=row, sticky='nswe')
            
            
    class App(tk.Tk):
        def __init__(self, **kwargs):
            tk.Tk.__init__(self)
            self.configure(**kwargs)
            
            cols = 2
            for i in range(cols):
                self.grid_columnconfigure(i, weight=1)
                
            rows = 2
            for i in range(rows):
                self.grid_rowconfigure(i, weight=1)
            
            for i, c in enumerate(['nw', 'ne', 'sw', 'se']):
                self.__dict__[f'widgets_{c}'] = Cell(self, i%cols, i//cols)
                
                
            self.widgets_nw['background'] = 'red'
                    
            
    if __name__ == '__main__':
        root = App(background="white", highlightbackground="black", highlightthickness=1)
        root.geometry('640x680+300+300')
        root.title('not Can is Should Example')
        root.mainloop()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-04-16
      • 2011-04-05
      • 1970-01-01
      相关资源
      最近更新 更多