Python TypeError: changeMaster.__init__() missing 1 required positional argument: 'parent'

Question:

I’ve looked at different questions and have looked at different resources to solve my problem but I haven’t been lucky as I’m fairly new with Python.
I’m trying to run my program at the very end with

if __name__ == "__main__":
    check = cursor.execute("SELECT * FROM masterpassword")
    # if there is already a present password 
    if check.fetchall(): 
        app = login()
    else: 
        app = changeMaster()

app.mainloop()
    

but I get an error that says:

TypeError: changeMaster.__init__() missing 1 required positional argument: 'parent'
# change master password with correct input 
class changeMaster(Toplevel):
    def __init__(self, parent):
        super().__init__(parent)
        
        # display config 
        self.geometry("400x400")
        self.title("change master password")

        # main menu button 
        back = Button(self, text="main menu", command=self.destroy).grid(padx=20, pady=30)

        # enter new password 
        Label(self, text="enter new password").place(x=250, y=300)
        self.newPass = Entry(width = 20)
        self.newPass.place(x=400, y=300)

        # re-enter password 
        Label(self, text="re-enter password").place(x=250, y=400)
        self.enter = Entry(width = 20)
        self.enter.place(x=400, y=400)
        
        # submit password 
        Button(self, text="submit", command=self.confirm).place(x=350, y=400)
    
    def confirm(self):
        # if the passwords match 
        if self.newPass.get() == self.enter.get():
            # new password is set to hashedPassword 
            hashedPassword = self.newPass.get()

            # inserts the new password into the database 
            insert_password = """INSERT INTO masterpassword(password)
            VALUES(?)"""
            
            # 
            cursor.execute(insert_password, [(hashedPassword)])
           
            # saves into database
            db.commit()

            # switch to the viewpassword window 
            window = viewPass(self)
            window.grab_set()
        else:
            Label(self, text="passwords do not match").place(x=375, y=500)

Any feedback would be greatly appreciated!

Asked By: sog71

||

Answers:

Toplevel is for creating second window and it needs main window as parent.

So first you have to create main window and later use it as parent

root = Tk()

app = changeMaster(root)

root.mainloop()

If it has to be then only window then you should use Tk (without parent) instead of Toplevel

class changeMaster(Tk):
    def __init__(self):
        super().__init__()    

    # ... code ...


app = changeMaster()

app.mainloop()
Answered By: furas
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.