Python:缺少1个必需的位置参数:'self'

3

我尝试在 Google 中找到解决方案,但是所有的解决方案对我来说都不起作用,也许在这里我可以得到正确的答案。

我有以下代码:

from tkinter import *


class gui_main:
    def __init__(self):

        self.win_about = Tk() #creat new windows

        self.win_about.title("About software") # Get title to new windows -> "About software"

        self.win_about.geometry("400x120+600+200") # Get size to window

        self.win_about.resizable(width=False, height=False) # Off option Increase / decrease window

        lab = Label(self.win_about, text="""
        welcome to app 
        For Enter to app click on button Next.
        """, justify='center') # Create new text in the window
        lab.place(x=-18, y=-11) # Position of text in window (x - up/down , y - left/right)

        btn_next = Button(self.win_about, text="Next", fg="red", command=gui_main.screen_menu()) # Create new button
        btn_next.place(x=350, y=90) # Position of button in window (x - up/down , y - left/right)

        btn_exit = Button(self.win_about, text="Exit", fg="red", command=self.win_about.destroy) # Create new button
        btn_exit.place(x=10, y=90) # Position of button in window (x - up/down , y - left/right)

        self.win_about.mainloop() # Run the cod in loop

    def screen_menu(self):
        self.win_menu = Tk()
        self.win_menu.title("Menu")
        self.win_menu.geometry("500x500+400+400")
        self.win_about.destroy()
        self.win_menu.mainloop()

if __name__ == "__main__":
    g = gui_main()
    g.win_about()

我收到的错误信息如下:

Traceback (most recent call last): File "文件路径", line 42, in g = gui_main() File "文件路径", line 26, in init btn_next = Button(self.win_about, text="下一页", fg="红色", command=gui_main.screen_menu()) # 创建新的按钮 TypeError: screen_menu()缺少1个必需的位置参数:'self'

感谢您的帮助,我希望能找到解决方案。


事件名称后面不应该有括号。 - Stefan
避免使用多个Tk()实例。 - acw1668
每次我想打开一个新窗口时,我需要一个新的Yk()吗? - chen sliktar
比较一下两个“Button”中的command是如何指定的。注意到了什么不同吗? - Karl Knechtel
2个回答

2
问题在于,你应该使用self而不是类名。除此之外,我和很多人都喜欢在command=中使用lambda方法。在类内部,必须使用self来调用每个方法和变量,以便在类内部工作。
除此之外,我也对您的代码进行了一些更改。因为在def __init__(self):方法中运行了mainloop(),所以您不需要调用g.about_screen(),因为当您初始化类对象时,它已经在运行了这里:g = class_name();.
以下是代码。
from tkinter import *


class gui_main:
    def __init__(self):

        self.win_about = Tk() #creat new windows

        self.win_about.title("About software") # Get title to new windows -> "About software"

        self.win_about.geometry("400x120+600+200") # Get size to window

        self.win_about.resizable(width=False, height=False) # Off option Increase / decrease window

        lab = Label(self.win_about, text="""
        welcome to app 
        For Enter to app click on button Next.
        """, justify='center') # Create new text in the window
        lab.place(x=-18, y=-11) # Position of text in window (x - up/down , y - left/right)

        btn_next = Button(self.win_about, text="Next", fg="red", command=lambda:self.screen_menu()) # Create new button
        btn_next.place(x=350, y=90) # Position of button in window (x - up/down , y - left/right)

        btn_exit = Button(self.win_about, text="Exit", fg="red", command=lambda:self.quit()) # Create new button
        btn_exit.place(x=10, y=90) # Position of button in window (x - up/down , y - left/right)

        self.win_about.mainloop() # Run the cod in loop
    def quit(self):
        self.win_about.quit()
    def screen_menu(self):
        self.win_menu = Tk()
        self.win_menu.title("Menu")
        self.win_menu.geometry("500x500+400+400")
        # self.win_about.destroy()
        self.win_menu.mainloop()

if __name__ == "__main__":
    g = gui_main()

非常感谢您的帮助。关于解释,如果我对这段代码有更多问题,我需要开新的问题还是在这里问?再次感谢您的帮助 :) - chen sliktar
在这里的注释中说了什么,让我看看是否能解决。我会很高兴帮忙。 - Search Imtiyaz

1
Button command 中删除括号,并引用 self 而不是 gui_main
btn_next = Button(self.win_about, text="Next", fg="red", command=self.screen_menu) # Create new button

您需要传递callable本身,而不是gui_main.screen_menu的返回值。

我删除了命令,现在不再出现错误提示,但程序无法运行。 - chen sliktar
看到我的更新,你需要引用 self.screen_menu - po.pe

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接