Tkinter Class structure (class per frame) issue with duplicating widgets
<p>Ive been trying out OOP for use with Tkinter - Im getting there (I think) slowly...</p>

<p>I wanted to build a structure where each frame is handled by its own class, including all of its widgets and functions. Perhaps I am coming from the wrong angle but that is what makes most logical sense to me. - Feel free to tell me if you agree / disagree!</p>

<p>I know why the problem is happening - when im calling each class my <code>__init__</code> runs everytime and builds the relevant widgets regardless of whether they are already present in the frame. However, the only way I can think of getting round this would be to build each frame in the <code>__init__</code> of my primary class <code>GUI_Start</code>. - Although this seems like a messy and un-organised soloution to the problem. </p>

<p>Is there a way I can achieve a structure where each class takes care of its own functions and widgets but doesn't build the frame each time?</p>

<p>See below for minimal example of the issue:</p>

<pre><code>from Tkinter import *

class GUI_Start:

    def __init__(self, master):
        self.master = master
        self.master.geometry('300x300')
        self.master.grid_rowconfigure(0, weight=1)
        self.master.grid_columnconfigure(0, weight=1)
        self.win_colour = '#D2B48C'
        self.frames = {}

        for window in ['win1', 'win2']:
            frame = Frame(self.master, bg=self.win_colour, bd=10, relief=GROOVE)
            frame.grid(row=0, column=0, sticky='news')
            setattr(self, window, frame)
            self.frames[window] = frame

        Page_1(self.frames)

    def Next_Page(self, frames, controller):
        controller(frames)


class Page_1(GUI_Start):

    def __init__(self, master):
        self.master = master
        self.master['win1'].tkraise()

        page1_label = Label(self.master['win1'], text='PAGE 1')
        page1_label.pack(fill=X)

        page1_button = Button(self.master['win1'], text='Visit Page 2...', command=lambda: self.Next_Page(self.master, Page_2))
        page1_button.pack(fill=X, side=BOTTOM)


class Page_2(GUI_Start):

    def __init__(self, master):
        self.master = master
        self.master['win2'].tkraise()

        page2_label = Label(self.master['win2'], text='PAGE 2')
        page2_label.pack(fill=X)

        page2_button = Button(self.master['win2'], text='Back to Page 1...', command=lambda: self.Next_Page(self.master, Page_1))
        page2_button.pack(fill=X, side=BOTTOM)


root = Tk()
gui = GUI_Start(root)
root.mainloop()
</code></pre>

<p>Feel free to critique the structure as I may be trying to approach this from the wrong angle!</p>

<p>Any feedback would be much appreciated!
Luke</p>

<p>The point of using classes is to encapsulate a bunch of behavior as a single unit. An object shouldn't modify anything outside of itself. At least, not by simply creating the object -- you can have methods that can have side effects.</p>

<p>In my opinion, the proper way to create "pages" is to inherit from <code>Frame</code>. All of the widgets that belong to the "page" must have the object itself as its parent. For example:</p>

<pre><code>class PageOne(tk.Frame):
    def __init__(self, parent):
        # use the __init__ of the superclass to create the actual frame
        tk.Frame.__init__(self, parent)

        # all other widgets use self (or some descendant of self)
        # as their parent

        self.label = tk.Label(self, ...)
        self.button = tk.Button(self, ...)
        ...
</code></pre>

<p>Once done, you can treat instances of this class as if they were a single widget:</p>

<pre><code>root = tk.Tk()
page1 = PageOne(root)
page1.pack(fill="both", expand=True)
</code></pre>

<p>You can also create a base <code>Page</code> class, and have your actual pages inherit from it, if all of your pages have something in common (for example, a header or footer)</p>

<pre><code>class Page(tk.Frame):
    def __init__(self, parent):
        tk.Frame.__init__(self, parent)
        &lt;code common to all pages goes here&gt;

class PageOne(Page):
    def __init__(self, parent):
        # initialize the parent class
        Page.__init__(self, parent)

        &lt;code unique to page one goes here&gt;
</code></pre>

python python-2.7 class oop tkinter