0

I am in the process of creating a UI using tkinter for a program I have written in Python2.7. I have modified code I found here: Switch between two frames in tkinter

I am having problems passing variables across instances. When I add the page1 argument to the __init__ method in the class page2, the following error message is returned:

Traceback (most recent call last):

File "question.py", line 77, in (module)

app = Setup()

File "question.py", line 16, in _ init _

frame = F(parent=container, controller=self)

TypeError: _ init _() takes exactly 4 arguments (3 given)

Below is a simplified version of my code:

import Tkinter as tk

class Setup(tk.Tk):

    def __init__(self, *args, **kwargs):
        tk.Tk.__init__(self, *args, **kwargs)

        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 (page1, page2):
            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("page1")

    def show_frame(self, page_name):
        frame = self.frames[page_name]
        frame.tkraise()

class page1(tk.Frame):

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

        tk.Label(self, text="Number of children:").grid(row=2, sticky=tk.E)
        self.ChildrenNo = tk.Entry(self)
        self.ChildrenNo.grid(row=2, column=1)

        Quit = tk.Button(self, text="Exit", command= self.quit)
        Quit.grid(row=13, column=0, sticky="sw")
        Next = tk.Button(self, text="OK",
                         command=lambda: controller.show_frame("page2"))
        Next.grid(row=13, column=1, sticky=tk.SE)
        Apply = tk.Button(self, text="Apply", command = self.varset1)
        Apply.grid(row=13, column=1, sticky=tk.SW)

        self.childno = 0
        self.rowno = 0

    def varset1(self):
        self.childrenno = self.ChildrenNo.get()

class page2(tk.Frame):

    def __init__(self, parent, controller, page1):
        tk.Frame.__init__(self, parent)
        self.controller = controller
        self.childrenno = page1.childrenno

        for i in range(self.childrenno):
            self.childno = self.childno + 1
            self.rowno = self.rowno + 1
            tk.Label(self, text="Age of child %r:" % (self.childno)).grid(row=2, sticky=tk.E)
            self.ChildrenNo = tk.Entry(self)
            self.ChildrenNo.grid(row=self.rowno, column=1)

        Quit = tk.Button(self, text="Exit", command= self.quit)
        Quit.grid(row=13, column=0, sticky="sw")
        Back = tk.Button(self, text="Back",
                         command=lambda: controller.show_frame("page1"))
        Back.grid(row=13, column=1, sticky=tk.SE)
        Apply = tk.Button(self, text="Apply", command = self.varset2)
        Apply.grid(row=13, column=1, sticky=tk.SW)

    def varset2(self):
        houseno = self.HouseNo.get()
        postcode = self.PostCode.get()

if __name__ == "__main__":
    app = Setup()
    app.mainloop()

Where might I be going wrong? Thanks in advance for any help.

Community
  • 1
  • 1
C. Pugh
  • 103
  • 2
  • 10
  • The whole point of the controller is so that each page doesn't have to know about the other pages. See this answer for an example of how to implement a `get_page` method: http://stackoverflow.com/a/32213127/7432 – Bryan Oakley Mar 23 '17 at 13:16
  • This is not how you use tkinter.... – Taku Mar 23 '17 at 13:17
  • @abccd: what does that mean? This is a perfectly acceptable way to use tkinter, though I don't like the fact someone used the pattern in a tutorial for beginners without properly explaining it, so now a lot of people are just copying the pattern without understanding it. For more information about where this particular pattern came from, start here: http://stackoverflow.com/questions/7546050 . – Bryan Oakley Mar 23 '17 at 13:18
  • @BryanOakley Thank you for the pointers. I plan to invest time in building a deeper understanding of how the code works to hopefully make things clearer. – C. Pugh Mar 23 '17 at 15:14

0 Answers0