Question

I'm trying to create a simple UI with Tkinter and I have run into a problem. My code looks like this:

class UIController(tk.Tk):
    def __init__(self, master=None):
        tk.Frame.__init__(self, master)
        container = tk.Frame(self)
        container.pack(side="top", fill="both", expand=True)
        self.frames = {}
        for F in (StartPage, BrowsePage, StudentPage):
            frame = F(self, container)
            self.frames[F] = frame
            frame.title("StudyApp")
        self.showFrame(StartPage)
        self.centerWindow()

    def showFrame(self, c):
        frame = self.frames[c]
        frame.tkraise()

    def centerWindow(self):
        w = 300
        h = 350
        sw = self.master.winfo_screenwidth()
        sh = self.master.winfo_screenheight()
        x = (sw - w)/2
        y = (sh - h)/2
        self.master.geometry('%dx%d+%d+%d' % (w, h, x, y))

class StartPage(tk.Frame):
    def __init__(self, master):
        tk.Frame.__init__(self, master)
        self.pack()

        self.L1 = Label(self, text="Search by credits:")
        self.L1.place(x=18, y=45)

        self.startYear = Entry(self, bd=2)
        self.startYear.place(x=20, y=70)
        self.startYear.bind("<Return>", View.enter(startYear.get()))

        self.quitButton = Button(self, text="Quit", command=sys.exit)
        self.quitButton.pack(side="bottom", padx=5, pady=5, fill=X)

        self.searchButton = Button(self, text="Search")
        self.searchButton.pack(side="bottom", padx=5, pady=0, fill=X)   

class BrowsePage(tk.Frame):
    def __init__(self, master):
        tk.Frame.__init__(self, master)

class StudentPage(tk.Frame):
    def __init__(self, master):
        tk.Frame.__init__(self, master)

root = tk.Tk()
root.resizable(width=False, height=False)
uicontrol = UIController(root)
root.mainloop()

It gives a TypeError that the constructor takes 2 arguments but 3 were given. What I'm trying to do is have the 3 pages (StartPage, BrowsePage and StudentPage) in the frame 'container', and bring them up as needed with button pushes and such. I don't understand why I'm getting this error.

EDIT:

Added the UIController call.

EDIT2:

Added the page classes StartPage, BrowsePage and StudentPage. The latter two classes are only husks at this point.

Was it helpful?

Solution

I think this is the line that is causing the issue, you cannot pass the self instance to the constructor.

frame = F(self, container)

Can you please check and add more information to the question to understand what you are trying to achieve.

OTHER TIPS

You can just add in __init__ one more argument, controller.

This works for me:

def __init__(self, master, controller):
    tk.Frame.__init__(self, master)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top