Search code examples
pythonpython-3.xtkintertoplevel

how to switch b/w the login window and main window by clicking the button in tkinter?


How to switch b/w the login window and main window? I know this types of questions are asked a lot but still i am not satisfied with the answer and please use method only helps me a lot.

from tkinter import *
def main_window():
    win2=Tk()
    label=Label(win2,text="helow").pack()
    Button(win2,text="logout",command=login_window).pack()
    win2.mainloop()

def login_window():
    win1=Tk()
    Label(win1,text="Password").pack()
    button1=Button(win1,text="click",command=main_window).pack()
    Entry(win1).pack()
    win1.mainloop()

login_window()

Solution

  • If you only want to use functions, this is what you can do:

    from tkinter import *
    
    root=Tk()
    
    def main_window(win1):
        win1.destroy()
        win2=Frame(root)
        win2.pack()
        label=Label(win2,text="helow").pack()
        Button(win2,text="logout",command=lambda:login_window(win2)).pack()
    
    def login_window(win2):
        win2.destroy()
        win1=Frame(root)
        win1.pack()
        Label(win1,text="Password").pack()
        button1=Button(win1,text="click",command=lambda:main_window(win1)).pack()
        Entry(win1).pack()
    
    def login_window1():
        win1=Frame(root)
        win1.pack()
        Label(win1,text="Password").pack()
        button1=Button(win1,text="click",command=lambda:main_window(win1)).pack()
        Entry(win1).pack()
    
    login_window1()
    
    root.mainloop()