Search code examples
pythonpython-3.xtkintertkinter-canvastkinter-entry

Unable to close the popup for Tkinter after entering the correct password


Unable to close the popup for Tkinter after entering the correct password. After entering the correct password the GUI is not closing.

from tkinter import *

root = Tk()

e = Entry(root, width=50)
e.pack()

def myClick():
    password = "sunny567"
    get = e.get()
    if get == password:
        myLabel = Label(root, text=get)
        myLabel.pack()

    else:
        myLabel = Label(root, text="Entered Password is wrong. Please try again.")
        myLabel.pack()

myButton = Button(root, text="Enter the password", command=myClick)
myButton.pack()

root.mainloop()

Solution

  • You need to call root.destroy() when password is correct.

    from tkinter import *
    
    root = Tk()
    
    e = Entry(root, width=50)
    e.pack()
    
    def myClick():
        password = "sunny567"
        get = e.get()
        if get == password:
            root.destroy()  # close the root window
        else:
            myLabel.config(text="Entered Password is wrong. Please try again.")
    
    myButton = Button(root, text="Enter the password", command=myClick)
    myButton.pack()
    
    myLabel = Label(root)
    myLabel.pack()
    
    root.mainloop()