title: How to properly access a StringVar() of a class from another class - Python - tkinter
body: <p>(I'm using mac 10.8.5 and Python3 with PyCharm)</p>
  
  <p>I have a tkinter GUI <code>TestMain()</code> class plus one <code>PageOne()</code> class and a <code>PageTwo()</code> class.
  I need <code>PageOne()</code> and <code>PageTwo()</code> to be different GUI windows cause they will handle different data. 
  I minimized the code in order to set it as readable as possible.
  After many tests I tried to place the <code>tk.StringVar()</code> and a simple function in the global scope as you can see below, but there's still a problem.</p>
  
  <pre><code>import tkinter as tk
  
  page1_label = tk.StringVar()
  page2_entry = tk.StringVar()
  
  def set_ebdata():
      data = page2_entry.get()
      page1_label.set(data)
  
  
  class TestMain(tk.Tk):
  
      def __init__(self, *args, **kwargs):
          tk.Tk.__init__(self, *args, **kwargs)
  
          tk.Tk.wm_title(self, 'TEST GUI')
          container = tk.Frame(self)
          container.pack(side='top')
          container.grid_rowconfigure(0, weight=1)
          container.grid_columnconfigure(0, weight=1)
          self.frames = {}
          for F in (PageOne, PageTwo):
              frame = F(container, self)
              self.frames[F] = frame
              frame.configure(background='lightgrey')
              frame.grid(row=0, column=0, sticky='nswe')
          self.show_frame(PageOne)
  
      def show_frame(self, cont):
          frame = self.frames[cont]
          frame.tkraise()
  
  
  class PageOne(tk.Frame):
  
      def __init__(self, parent, controller):
          tk.Frame.__init__(self, parent)
  
          frame_eb_data = tk.Frame(self, width=100, height=100, bg="orange", colormap="new")
          frame_eb_data.grid(row=0, column=0, sticky='w', padx=5, pady=5)
          frame_but_right = tk.Frame(self, width=240, height=60, bg="yellow", colormap="new")
          frame_but_right.grid(row=0, column=1, padx=5, pady=5, rowspan=2)
          lab_eb_data = tk.Label(frame_eb_data, background='#DDD4EF', textvariable=page1_label)
          lab_eb_data.grid(row=0, column=0, sticky='n')
  
          b_ebdata = tk.Button(frame_but_right, text="Page 2", width=10, height=2, command=lambda: controller.show_frame(PageTwo))
          b_ebdata.grid(row=3, column=0)
  
  
  class PageTwo(tk.Frame):
  
      def __init__(self, parent, controller):
          tk.Frame.__init__(self, parent)
  
          frame_buttons = tk.Frame(self, width=455, bg="#DDD4EF", colormap="new")
          frame_buttons.grid(row=0, column=0, padx=5, pady=5, sticky='e')
          frame_up_left = tk.Frame(self, width=485, height=260, bg="#89E3FA", colormap="new")
          frame_up_left.grid(row=1, column=0, sticky='w', padx=5, pady=5)
  
          b_data = tk.Label(frame_buttons, text='Example GUI', font='TrebuchetMS 30 bold', background="#DDD4EF")
          b_data.grid(row=0, column=0, padx=13, pady=5, sticky='w')
  
          b5 = tk.Button(frame_buttons, text='Set Text', command=lambda: set_ebdata)
          b5.grid(row=0, column=2, padx=5, pady=5, sticky='e')
  
          b6 = tk.Button(frame_buttons, text='Page 1', command=lambda: controller.show_frame(PageOne))
          b6.grid(row=0, column=3, padx=5, pady=5, sticky='e')
  
          label_2 = tk.Label(frame_up_left, text="Name:", font=("bold", 14))
          label_2.grid(row=1, column=0, sticky='e')
          entry_nombre_fld = tk.Entry(frame_up_left, width=40, textvariable=page2_entry)
          entry_nombre_fld.grid(row=1, column=1, columnspan=3, sticky='w')
  
  
  app = TestMain()
  
  app.mainloop()
  </code></pre>
  
  <p>When you run the program a window with a "Page 2" button (<code>b_ebdata</code>) appears, by clicking it you enter Page 2 window which has a "Set Text" button (<code>b5</code>), a "Page 1" button (<code>b6</code>) and an entry field (<code>entry_nombre_fld</code>).</p>
  
  <p>I'd like to set the text I'll enter in the entry field (<code>entry_nombre_fld</code>) in the Page 1 label (<code>lab_eb_data</code>) by clicking the "Set Text" button (<code>b5</code>).</p>
  
  <p>Could a solution be to put <code>page1_label = tk.StringVar()</code> into <code>PageOne()</code> class and <code>page2_entry = tk.StringVar()</code> into <code>PageTwo()</code> class and make those accessible by each other?</p>
  
  <p>Any other suggestion ?</p>
  
  <p>Thx in advance for your help!</p>