0

I am creating a login/sign-up system using tkinter. When the user clicks on either login or sign-up, I want all widgets to disappear for new widgets to appear on the screen depending on if they clicked on login or sign-up. So if they clicked on login, new labels and textboxes would appear for their username and password. The problem is I am using .place() and the tutorials I have seen are mostly using pack_forget or grid_forget

My Code:

from tkinter import *


class Window:

    def __init__(self, master):

        root.title("Sign Up or Login")
        root.minsize(width=300, height=300)
        root.maxsize(width=300,height=300)

        self.login_button = Button(master, text = "Login", width=18,height=4, command=self.LoginPage)
        self.signup_button = Button(master, text = "Sign Up", width=18,height=4, command=self.SignupPage)

        self.login_button.place(relx=0.5, rely=0.3, anchor=CENTER)
        self.signup_button.place(relx=0.5, rely=0.7, anchor=CENTER)

    def LoginPage(self):
        root.title("Login")

    def SignupPage(self):
        root.title("Sign Up")



root = Tk()

run = Window(root)

root.mainloop()

My Interface:

Interface

1 Answers1

1

No matter you use place,pack or grid. The best solution works for all:

for widgets in root.winfo_children():
    widgets.destory()

It loops through widgets and delete them. You can try:

from tkinter import *


class Window:

    def __init__(self, master):

        root.title("Sign Up or Login")
        root.minsize(width=300, height=300)
        root.maxsize(width=300,height=300)

        self.login_button = Button(master, text = "Login", width=18,height=4, command=self.LoginPage)
        self.signup_button = Button(master, text = "Sign Up", width=18,height=4, command=self.SignupPage)

        self.login_button.place(relx=0.5, rely=0.3, anchor=CENTER)
        self.signup_button.place(relx=0.5, rely=0.7, anchor=CENTER)

    def LoginPage(self):
        root.title("Login")
        self.Restore()
    def SignupPage(self):
        root.title("Sign Up")
        self.Restore()
    def Restore(self):
            for widgets in root.winfo_children():
                widgets.destroy()


root = Tk()

run = Window(root)

root.mainloop()
Nouman
  • 6,947
  • 7
  • 32
  • 60