Python: missing 1 required positional argument: ‘self’

pythonpython-3.xtkinter

I try find solution in google but all solution in google not work for me maybe here I get The correct answer.

I have this code:

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()

And I get this Error:

Traceback (most recent call last): File "location file", line 42, in
g = gui_main() File "location file", line 26, in init
btn_next = Button(self.win_about, text="Next", fg="red", command=gui_main.screen_menu()) # Create new button TypeError: screen_menu() missing 1 required positional argument: 'self'

Ty for help, I will hope find any solution

Best Answer

the problem is you should use self instead of class name apart from that i prefer and many people prefer to use lambda methods in command=. inside class you have to call each methods and variables with self. in order to work inside class.
apart from that i made few changes in your code also. you don't need to call g.about_screen() since you running mainloop() inside def __init__(self): method. because its already running when you are initializing the class object here g = class_name();.
here is the code

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()