Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/317.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Tkinter的问题-继承问题_Python_Class_Tkinter - Fatal编程技术网

Python Tkinter的问题-继承问题

Python Tkinter的问题-继承问题,python,class,tkinter,Python,Class,Tkinter,我开始使用Tkinter应用程序处理类,但我似乎不了解类是如何工作的,尤其是关系父控制器。正如您在下面的代码中所看到的,我计划为整个部分创建一个外部类,然后为该框架内的每个部分创建4个内部类。但是,我无法从初始帧调用这些类。有没有更好的办法?我做错了什么 class MainScreenFrameCenter(tk.Frame): def __init__(self, parent, controller,*args,**kwargs): tk.Frame.__init_

我开始使用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") 

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

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

似乎您正在尝试制作一个小网格。类通常不嵌套在另一个类中。如果创建一个表示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()

“我不能从初始帧调用这些类。”-为什么不能?你说的“叫那些课”是什么意思?当您尝试时会发生什么?您是否有意将这些其他类放入
ManScreenFrameCenter
?这是非常不寻常的。例如,它说MainScreenFrameCenterNW不存在。FrameCenter基本上是一个较大的框架,分为4个部分。我把它们放在MainScreenFrameCenter中,因为它们属于那个框架,我应该把它们放在外面吗?