title: Dynamically instantiate pages Tkinter
body: <p>I'm building a GUI with code that originally came from another stack exchange answer. I've modified it since I want to pass variables to following pages and have them display the values. As a way to do this, rather than display on a button event, I am trying to create the page with the show() method:</p>
  <pre><code>import Tkinter as tk
  #import vpnRename12 as Rename
  #import vpnRename_pullconfig as pullConfig
  
  &quot;&quot;&quot;
  Create multiple pages to go through in the same frame. Use lift() to bring
  the desired page into view and stack them on top of one another. Define
  page-specific methods in each page.
  &quot;&quot;&quot;
  
  class vpnRenameProgram(tk.Tk):
      &quot;&quot;&quot;
      Create a page class that will allow you to lift to front the applicable
      page with the proper functions to keep the script running.
      &quot;&quot;&quot;
      def __init__(self,*args,**kwargs):
      
          tk.Tk.__init__(self,*args,**kwargs)
          
          # Create an empty dictionary to contain all of the page names
          self.frames = {}
      
          self.show(&quot;MainView&quot;)
  
      # Instantiate the next page on call, rather than at start to allow 
      flexibility
      def instantiate_page(self, cls):
          &quot;&quot;&quot;
          Since all of the pages are defined in the same scope/namespace, we 
          can use
          the globals()[] dict to find and instantiate the pages dynamically 
          with the show() method.
          cls is the class argument we are doing a lookup on in the global() 
          dict. 
          &quot;&quot;&quot;
      
          try:
              newframe = globals()[cls](container,self)
              page_name = newframe.__name__
          except:
              print(&quot;\nError defining inline class %s&quot;%cls)#(&quot;Class %s is not defined&quot; %cls)
              newframe = None
              page_name=globals()[cls].__name__
          
          return newframe, page_name
  
      # Create lift function to bring desired page to front of view, 
      #instantiate page if
      # it isn't already (check frames dict)
      def show(self, cls):
          if cls not in self.frames.keys():
              container = tk.Frame(self)
              container.pack(side=&quot;top&quot;, fill=&quot;both&quot;, expand=True)
              container.grid_rowconfigure(0, weight=1)
              container.grid_columnconfigure(0, weight=1)
      
              frame, page_name = self.instantiate_page(cls)
          
              if frame==None:
                  frame = globals()[cls](parent=container, controller=self)
          
              self.frames[page_name] = frame
              frame.grid(row=0, column=0, sticky=&quot;news&quot;)
      
          frame = self.frames[cls]
          frame.lift()
      
      def get_page(self, classname):
          &quot;&quot;&quot;
          Return instance of page when it's class name is passed in as string
          &quot;&quot;&quot;
          for page in self.frames.values():
              if str(page.__class__.__name__) == classname:
                  return page
              return None
  
  class MainView(tk.Frame):
  
      def __init__(self,parent, controller,**kwargs):
          tk.Frame.__init__(self,parent)
          self.controller = controller
          self.edit_directory=&quot;edit_dir&quot;
          self.complete_directory =&quot;comp_dir&quot;
      
          TitleLabel = tk.Label(self, text=&quot;VPN Script Editor&quot;)
          TitleLabel.pack({&quot;side&quot;:&quot;top&quot;})
      
          EditLabel = tk.Label(self, text=&quot;Edit File Directory: 
              %s&quot;%self.edit_directory)
          EditLabel.pack()
      
          CompLabel = tk.Label(self, text=&quot;Completed File Directory: 
              %s&quot;%self.complete_directory)
          CompLabel.pack()
      
          Next = tk.Button(self, text=&quot;Next&quot;, command=lambda: 
              controller.show(&quot;listVPN&quot;))
          Next.pack()
      
      
  class listVPN(tk.Frame):
      &quot;&quot;&quot;
      This is the second page, it contains a text box where you will list the 
      names of the vpn's that you want to edit. It will also display the 
      directories
      obtained by the pullconfig script.
      &quot;&quot;&quot;
  
      def read_list(self):
          vpn_list=str(self.var_vpn_list.get()).upper()
          return vpn_list
  
      def __init__(self, parent, controller, **kwargs):
          self.controller = controller
          tk.Frame.__init__(self, parent)
      
          self.var_vpn_list = tk.StringVar()
      
      
          label=tk.Label(self, text=&quot;Please list the VPNs desired to edit&quot;)
          label.pack()
      
          #Create text box to submit a list of vpns back to the main program
          vpnLabel = tk.Label(self, text=&quot;VPN Names&quot;).pack()
          self.TextBox = tk.Entry(self, textvariable=self.var_vpn_list)
          self.TextBox.pack()
      
          vpnListSubmit = tk.Button(self, text=&quot;Enter&quot;, command= lambda: 
          self.read_list() and self.controller.show(&quot;pickFiles&quot;))
          vpnListSubmit.pack()
          
      
  class pickFiles(tk.Frame):
      &quot;&quot;&quot;
      Second page that allows you to select your desired files from the 
      edit directory specified in the config file. Check all desired files, 
      list will be returned to the program.
      &quot;&quot;&quot;
  
      def get_vpn_list(self):
          list = self.controller.get_page(&quot;listVPN&quot;).var_vpn_list.get()
          self.vpn_list = str(list).upper()
      
          self.vpn_label.configure(text=&quot;VPN List: %s&quot;%self.vpn_list)
      
          return self.vpn_list
  
      def __init__(self, parent, controller,**kwargs):
  
          # Inherits from the tk.Frame class 
          tk.Frame.__init__(self, parent)
          self.controller = controller
          self.vpn_list = tk.StringVar()
      
          list = self.controller.get_page(&quot;listVPN&quot;).var_vpn_list.get()
          self.vpn_list = str(list).upper()
      
      
      
          show_vpn = tk.Button(self, text=&quot;Show vpnlist&quot;, command = 
              self.get_vpn_list)
          show_vpn.pack()
      
          self.vpn_label = tk.Label(self, text=&quot;VPN List: %s&quot; %self.vpn_list)
          self.vpn_label.pack()
      
          # todo: get external module function to run with variable input
          #file_list = Rename.searchFile(vpnlist)
  
  # Execute program on calling the parent class       
  if __name__==&quot;__main__&quot;:
      app = vpnRenameProgram()
      app.mainloop()
  </code></pre>
  <p>EDIT:
  Above is my whole code with custom scripts I've imported commented out. My main question is about layout. I want the frames to stack on top of one another, but they are not. Why is it doing this and what would get me on track to getting the layout I want?</p>