错误! tkinter.TclError:未知选项" -parent"当打开一个框架Tkinter Py3.6

时间:2017-04-06 16:24:03

标签: python-3.x tkinter

好的,我正在重写和练习一些代码,以便学习代码和代码的功能。现在我对这里发生的事情有了一个很好的理解,但是我继续使用这组特殊代码得到这个错误(如下所示)。

它没有与其他人一起做,除变量名之外几乎完全相同。但是,我认为这是问题,我已经改变了一个变量名称,这是一个内置变量。

例如,在下面的代码之前,我有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 个答案:

答案 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