Python:__init__() 需要 2 个位置参数,但给出了 3 个

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22596549/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 01:15:09  来源:igfitidea点击:

Python: __init__() takes 2 positional arguments but 3 were given

pythontkintertypeerror

提问by jonraem

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

我正在尝试使用 Tkinter 创建一个简单的 UI,但遇到了问题。我的代码如下所示:

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.

它给出了一个 TypeError 构造函数接受 2 个参数但给出了 3 个参数。我想要做的是在框架“容器”中包含 3 个页面(StartPage、BrowsePage 和 StudentPage),并根据需要通过按钮等操作将它们调出。我不明白为什么我会收到这个错误。

EDIT:

编辑:

Added the UIController call.

添加了 UIController 调用。

EDIT2:

编辑2:

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

添加了页面类 StartPage、BrowsePage 和 StudentPage。后两个类此时只是外壳。

采纳答案by asp

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

我认为这是导致问题的行,您不能将 self 实例传递给构造函数。

frame = F(self, container)

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

您能否检查并在问题中添加更多信息以了解您要实现的目标。

回答by Ν?κο? Στεφαν?πουλο?

You can just add in __init__one more argument, controller.

您可以再添加__init__一个参数,controller.

This works for me:

这对我有用:

def __init__(self, master, controller):
    tk.Frame.__init__(self, master)