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.
The Python "TypeError: takes 2 positional argument but 3 were given" occurs for multiple reasons: Forgetting to specify the self argument in a class method. Forgetting to specify a third argument in a function's definition. Passing three arguments to a function that only takes two.
How to fix TypeError: method() takes 1 positional argument but 2 were given. In our above code, we have not passed the self argument to the method defined in the Employee class, which leads to TypeError. As shown below, we can fix the issue by passing the “ self ” as a parameter explicitly to the GetEmployeeID() method ...
Positional Arguments in Python A positional argument in Python is an argument whose position matters in a function call. Let's define a function that shows info about a person given the name and age: def info(name, age): print(f"Hi, my name is {name}.
Positional arguments must be passed in order as declared in the function. So if you pass three positional arguments, they must go to the first three arguments of the function, and those three arguments can't be passed by keyword.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With