Naseem Tomkinson
Naseem Tomkinson

Reputation: 119

Error! tkinter.TclError: unknown option "-parent" when opening a frame Tkinter Py3.6

Okay, so i'm rewriting and practising some code, in order to learn the code and what this code does. Now i have a decent understanding of whats going on here, however i keep getting this error (shown below) with this particular set of code.

It doesn't do it with the others and there pretty much exactly the same apart from the variable names. However i think that is the problem, ive changed a variable name that is a built in variable as such.

For example prior to the code below i had frameContainer rather than just container, and F rather than 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"

This is my written code.

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()

Other code if you would like to take a look

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)

Upvotes: 0

Views: 1112

Answers (1)

Bryan Oakley
Bryan Oakley

Reputation: 386010

Just like the error states, parent is not a valid option for a Frame.

The reason it happens for some windows and not others is that one window explicitly declares a parent parameter whereas the others do not:

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

class overview_frame(tk.Frame):
  pass

class accounting_frame(tk.Frame):
  pass

Upvotes: 1

Related Questions