2017-04-06 85 views
0

好吧,所以我重写和练习一些代码,以了解代码以及代码的功能。现在,我对这里发生的事情有了一个体面的理解,但是我一直在用这组特定的代码得到这个错误(如下所示)。错误! tkinter.TclError:未知的选项“-parent”打开框架时Tkinter Py3.6

它不会与其他人一起完成,除了变量名之外,它们几乎完全相同。但是我认为这是问题,我改变了一个变量名,这是一个内置变量。

例如下面我有frameContainer而不仅仅是container,并F而不是n_frame

Traceback (most recent call last): 
    File "C:/Users/nasto/Desktop/CMS.py", line 40, in <module> 
    app = frame_store() 
    File "C:/Users/nasto/Desktop/CMS.py", line 14, in __init__ 
    frame = n_frame(parent=container, controller=self) 
    File "C:\Users\nasto\AppData\Local\Programs\Python\Python36-32\lib\tkinter\__init__.py", line 2738, in __init__ 
    Widget.__init__(self, master, 'frame', cnf, {}, extra) 
    File "C:\Users\nasto\AppData\Local\Programs\Python\Python36-32\lib\tkinter\__init__.py", line 2293, in __init__ 
    (widgetName, self._w) + extra + self._options(cnf)) 
_tkinter.TclError: unknown option "-parent" 

这是我写的代码的代码之前。

import tkinter as tk 

class frame_store(tk.Tk): 
    def __init__(self, *args, **kwargs): 
    tk.Tk.__init__(self, *args, **kwargs) 
    '''container is used so that all the frames can be stacked, from there we can call other frames within the container to the top, to the current active frame''' 
    container = tk.Frame(self) 
    container.pack(side='top', fill='both', expand=True) 
    container.grid_rowconfigure(0, weight=1) 
    container.grid_columnconfigure(0, weight=1) 
    self.frames = {} 
    for n_frame in (login_frame, overview_frame, accounting_frame): 
     pageName = n_frame.__name__ 
     frame = n_frame(parent=container, controller=self) 
     self.frames[pageName] = frame 
     frame.grid(row=0, column=0, sticky='nsew') 
    self.show_frames('login_frame') 

    def show_frames(self, pageName): 
    frame = self.frames[pageName] 
    self.update_idletasks() 
    self.state('zoomed') 
    self.title('a title') 
    frame.tkraise() 

class login_frame (tk.Frame): 
    def __init__(self, parent, controller): 
    tk.Frame.__init__(self, parent) 
    self.controller = controller 
    labelOne = tk.Label(self, text='Start Frame').grid(row=1, column=1) 


class overview_frame(tk.Frame): 
    pass 

class accounting_frame(tk.Frame): 
    pass 

if __name__ == "__main__": 
    app = frame_store() 
    app.mainloop() 

,如果你想看看

class SampleApp(tk.Tk): 
    def __init__(self, *args, **kwargs): 
     tk.Tk.__init__(self, *args, **kwargs) 
     # the container is where we'll stack a bunch of frames 
     # on top of each other, then the one we want visible 
     # will be raised above the others 
     container = tk.Frame(self) 
     container.pack(side="top", fill="both", expand=True) 
     container.grid_rowconfigure(0, weight=1) 
     container.grid_columnconfigure(0, weight=1) 
     self.frames = {} 
     for F in (StartPage, PageOne, PageTwo): 
      page_name = F.__name__ 
      frame = F(parent=container, controller=self) 
      self.frames[page_name] = frame 
      frame.grid(row=0, column=0, sticky="nsew") 

     self.show_frame("StartPage") 

    def show_frame(self, page_name): 
     '''Show a frame for the given page name''' 
     frame = self.frames[page_name] 
     self.update_idletasks() 
     self.state('zoomed') 
     self.title('A2 Project Finances') 
     frame.tkraise() 


class StartPage(tk.Frame): 
    def __init__(self, parent, controller): 
     tk.Frame.__init__(self, parent) 
     self.controller = controller 

     self.userID = tk.Button(self, text = 'User ID ').grid(row=1,column=0) 

回答

1

就像错误状态为其他代码,parent不是一个Frame的有效选项。

它发生了一些窗口,而不是其他的原因是,一个窗口明确声明了parent参数,而有的则没有:

class login_frame (tk.Frame): 
    def __init__(self, parent, controller): 
    ... 

class overview_frame(tk.Frame): 
    pass 

class accounting_frame(tk.Frame): 
    pass 
+0

生命的救星,感谢队友! –