Py2.7: Tkinter have code for pages in separate files
<p>I have code that opens up a window and has three buttons for page 1, page 2, and page 3. However I also have Four .py files (Dashboard, PageOne, PageTwo. PageThree). Dashboard will be the file that used to run the application. I want it so that the code from each file is only runs/displayed when the user clicks on the corresponding page button. </p>

<p>Dashboard.py:</p>

<pre><code>import Tkinter as tk
import PageOne
import PageTwo
import PageThree

class Page(tk.Frame):
    def __init__(self, *args, **kwargs):
        tk.Frame.__init__(self, *args, **kwargs)
    def show(self):
        self.lift()

class MainView(tk.Frame):
    def __init__(self, *args, **kwargs):
        tk.Frame.__init__(self, *args, **kwargs)
        p1 = Page1(self)
        p2 = Page2(self)
        p3 = Page3(self)

        buttonframe = tk.Frame(self)
        container = tk.Frame(self)
        buttonframe.pack(side="top", fill="x", expand=False)
        container.pack(side="top", fill="both", expand=True)

        p1.place(in_=container, x=0, y=0, relwidth=1, relheight=1)
        p2.place(in_=container, x=0, y=0, relwidth=1, relheight=1)
        p3.place(in_=container, x=0, y=0, relwidth=1, relheight=1)

        b1 = tk.Button(buttonframe, text="Page 1", command=p1.lift)
        b2 = tk.Button(buttonframe, text="Page 2", command=p2.lift)
        b3 = tk.Button(buttonframe, text="Page 3", command=p3.lift)

        b1.pack(side="left")
        b2.pack(side="left")
        b3.pack(side="left")

        p1.show()

if __name__ == "__main__":
    root = tk.Tk()
    main = MainView(root)
    main.pack(side="top", fill="both", expand=True)
    root.wm_geometry("400x400")
    root.mainloop()
</code></pre>

<p>PageOne.py:</p>

<pre><code>import Tkinter as tk
import Dashboard

class Page1(Page):
   def __init__(self, *args, **kwargs):
       Page.__init__(self, *args, **kwargs)
       label = tk.Label(self, text="This is page 1")
       label.pack(side="top", fill="both", expand=True)
</code></pre>

<p>PageTwo.py</p>

<pre><code>import Tkinter as tk
import Dashboard

class Page2(Page):
   def __init__(self, *args, **kwargs):
       Page.__init__(self, *args, **kwargs)
       label = tk.Label(self, text="This is page 2")
       label.pack(side="top", fill="both", expand=True)
</code></pre>

<p>PageThree.py:</p>

<pre><code>import Tkinter as tk
import Dashboard

class Page3(Page):
   def __init__(self, *args, **kwargs):
       Page.__init__(self, *args, **kwargs)
       label = tk.Label(self, text="This is page 3")
       label.pack(side="top", fill="both", expand=True)
</code></pre>

<p>EDIT:</p>

<p>Error I'm getting:</p>

<pre><code>Traceback (most recent call last):
  File "C:\Users\ross.watson\Google Drive\Smart Mirror\Test\Dashboard.py", line 2, in &lt;module&gt;
    import PageOne
  File "C:\Users\ross.watson\Google Drive\Smart Mirror\Test\PageOne.py", line 2, in &lt;module&gt;
    import Dashboard
  File "C:\Users\ross.watson\Google Drive\Smart Mirror\Test\Dashboard.py", line 3, in &lt;module&gt;
    import PageTwo
  File "C:\Users\ross.watson\Google Drive\Smart Mirror\Test\PageTwo.py", line 4, in &lt;module&gt;
    class Page2(Page):
NameError: name 'Page' is not defined
</code></pre>

<p>The error messages tell you everything you need to know in order to fix the problem. This is a simple problem that has nothing to do with tkinter, and everything to do with properly organizing and using your code. </p>

<p>For example, what is <code>Page is not defined</code> telling you? It's telling you, quite literally, that <code>Page</code> is not defined. You defined it in your main file, but you're using it in another file. The solution is to move the definition of <code>Page</code> to a separate file that can be imported into the files that use it.</p>

<p>Modify your files and imports to look like this:</p>

<h2>page.py</h2>

<pre><code>class Page(tk.Frame):
    ...
</code></pre>

<h2>pageOne.py</h2>

<pre><code>from page import Page
class PageOne(Page):
    ...
</code></pre>

<h2>pageTwo.py</h2>

<pre><code>from page import Page
class PageTwo(Page):
    ...
</code></pre>

<h2>pageThree.py</h2>

<pre><code>from page import Page
class PageThree(Page):
    ...
</code></pre>

<h2>Dashboard.py</h2>

<pre><code>from pageOne import PageOne
from pageTwo import PageTwo
from pageThree import PageThree
...
</code></pre>

python python-2.7 tkinter