Search code examples
pythontkinterwindow

Creating a Tkitner application with multiple windows


I want to be able to open up another window and destroy the current login window, only once the login details are correct.

I have set the current login window as the top level and withdrew the root window, I'm guessing I now need to destroy the Toplevel and recall the root, but the exact method of doing this I am unsure about. Any help would be appreciated.

from Tkinter import *
import tkMessageBox
import time

#Login details
username = ("admin")
password = ("")

#Login attempt
def try_login():
    print("Trying to login...")
    if username_guess.get() == username:
        tkMessageBox.showinfo("COMPLETE", "You Have Now Logged In.", icon="info")
    else:
        tkMessageBox.showinfo("-- ERROR --", "Please enter valid infomation!", icon="warning")



#Gui Things
root = Tk()
top = Toplevel()
top.configure(bg='lightgrey')
top.resizable(width=FALSE, height=FALSE)
top.title("Log-In")
top.geometry("200x160")
# change window icon
top.wm_iconbitmap(r'C:\Python27\py.ico')
top.wm_title('APP')

#Time and Date
time1 = ''
clock = Label(top, font=('times', 10, 'bold'), bg='lightgrey')
clock.pack(side="bottom", fill='both', expand=False, ipadx=0, ipady=0)


def tick():
    global time1
    time2 = time.strftime("%d-%m-%Y %H:%M")
    if time2 != time1:
        time1 = time2
    clock.config(text=time2)
    clock.after(200, tick)
tick()



#Creating the username & password entry boxes
username_text = Label(top, text="Username:", bg='lightgrey')
username_guess = Entry(top)
password_text = Label(top, text="Password:", bg='lightgrey')
password_guess = Entry(top, show="*")
#attempt to login button
attempt_quit = Button(top, text="Quit", command=top.destroy).pack(side="bottom", fill='both', expand=False, padx=0, pady=0)
attempt_login = Button(top, text="Login", command=try_login).pack(side="bottom", fill='both', expand=False, padx=0, pady=0)




username_text.pack()
username_guess.pack()
password_text.pack()
password_guess.pack()

#Hides the root window
root.withdraw()

#Top Starter
top.mainloop()

Solution

  • if username_guess.get() == username: tkMessageBox.showinfo("COMPLETE", "You Have Now Logged In.", icon="info")

    After this line, you can call root.deiconify() to show the root window, and top.destroy() to kill the login window.