Search code examples
python-3.xuser-interfacetkinteraccount

How can i edit this code i've made so far so that when i click the button 'signup' it closes that gui and opens the next one


i am using tkinter to make a gui and have made various different buttons and now i have made all this i am unsure how to correctly make the first gui box close as the second one opens (sign_in function)

from tkinter import *
class login:
    def __init__(self, master):
        frame = Frame(master)
        frame.grid()
        self.button1 = Button(frame, text="signup", fg="green",command=self.sign_in)
        self.button2 = Button(frame, text="sign in", fg="black",)
        self.button3 = Button(frame, text="quit", fg="red", command=frame.master.destroy)
        self.button1.grid(stick=W)
        self.button2.grid(stick=W)
        self.button3.grid(stick=W)


    def sign_in(self):

        frame = Frame()
        frame.grid()
        name = Label(root, text="Name: ")
        password = Label(root, text="password: ")
        entry1 = Entry(root)
        entry2 = Entry(root)
        name.grid(row=0, sticky=E)
        password.grid(row=1, sticky=E)
        entry1.grid(row=0, column=1)
        entry2.grid(row=1, column=1)

        c = Checkbutton(root, text="keep me logged in")
        c.grid(columnspan=2, sticky="w")



root = Tk()
account=login(root)
root.mainloop()

Solution

  • Your code contains some indentation errors so I'll just go by your question.

    when i click the button 'signup' it closes that gui and opens the next one

    You can do so by first withdrawing your root window like this: root.withdraw() which will hide your original window. Then create a Toplevel window like this: newWindow = tk.Toplevel(root) to create a new window. You will just need to place these lines in the button command call.

    Here's what you can change in the sign_in note that I changed all the masters to frame and not root:

    def sign_in(self):
        root.withdraw()
        frame = Toplevel(root)
        name = Label(frame, text="Name: ")
        password = Label(frame, text="password: ")
        entry1 = Entry(frame)
        entry2 = Entry(frame)
        name.grid(row=0, sticky=E)
        password.grid(row=1, sticky=E)
        entry1.grid(row=0, column=1)
        entry2.grid(row=1, column=1)
    
        c = Checkbutton(frame, text="keep me logged in")
        c.grid(columnspan=2, sticky="w")