Python:__init__() 需要 2 个位置参数,但给出了 3 个 [英] Python: __init__() takes 2 positional arguments but 3 were given
问题描述
我正在尝试使用 Tkinter 创建一个简单的 UI,但遇到了一个问题.我的代码如下所示:
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()
它给出了一个 TypeError 构造函数接受 2 个参数但给出了 3 个参数.我想要做的是在框架容器"中包含 3 个页面(StartPage、BrowsePage 和 StudentPage),并根据需要通过按钮等操作将它们调出.我不明白为什么我会收到这个错误.
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.
添加了 UIController 调用.
Added the UIController call.
编辑 2:
添加了页面类 StartPage、BrowsePage 和 StudentPage.后两个类此时只是外壳.
Added the page classes StartPage, BrowsePage and StudentPage. The latter two classes are only husks at this point.
推荐答案
我认为这是导致问题的行,您不能将 self 实例传递给构造函数.
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.
这篇关于Python:__init__() 需要 2 个位置参数,但给出了 3 个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!