function' object has no attribute 'destroy'

It says function’ object has no attribute ‘destroy
When I press the register button, the register wwindow could not be destory and goes to the next page.

Designing for registration

def register():
global register_screen
register_screen = Toplevel(root)
register_screen.title("Register")
register_screen.geometry("300x250")

global username
global password
global username_entry
global password_entry
username = StringVar()
password = StringVar()

Label(register_screen, text="Please enter details below").pack()
Label(register_screen, text="").pack()
username_lable = Label(register_screen, text="Username")
username_lable.pack()
username_entry = customtkinter.CTkEntry(master=register_screen,textvariable=username, placeholder_text="Username")
username_entry.pack(padx=20, pady=10)
password_lable = Label(register_screen, text="Password")
password_lable.pack()
password_entry = customtkinter.CTkEntry(master=register_screen,textvariable=password, placeholder_text="Password")
password_entry.pack(padx=20, pady=10)
Label(register_screen, text="").pack()
register_button_1= customtkinter.CTkButton(master=register_screen, text="Register", command = onClick,height = 35, width = 250 )
register_button_1.place(relx=0.5, rely=0.8, anchor=tkinter.CENTER)

#When the register button is click the emial and the passwrod will be sotre in a text file 
def onClick():
f = open('hi.txt','a')
f.write(username.get()+' '+password.get()+'\n')
f.close()
register.destroy()
login()

>Solution :

register is a function you defined at the top of the snippet, and as the error says, it has no attribute called destroy. It seems like you meant to call register_screen.destroy().

Leave a Reply