Reputation: 854
I am making a makeshift sign in system with python. Currently if you enter the correct password it brings up a new admin window. If you enter the wrong one it brings up a new window that says wrong password. If you exit out of one of those windows and then try to enter a password again it breaks. tkinter.TclError: can't invoke "wm" command: application has been destroyed
Is there a way to prevent this so if someone enters the wrong password they don't need to restart the app?
import tkinter
from tkinter import *
#define root window
root = tkinter.Tk()
root.minsize(width=800, height = 600)
root.maxsize(width=800, height = 600)
#define admin window
admin = tkinter.Tk()
admin.minsize(width=800, height = 600)
admin.maxsize(width=800, height = 600)
admin.withdraw()
#define wrong window
wrong = tkinter.Tk()
wrong.minsize(width=200, height = 100)
wrong.maxsize(width=200, height = 100)
wrong.withdraw()
Label(wrong, text="Sorry that password is incorrect!", font=("Arial", 24), anchor=W, wraplength=180, fg="red").pack()
#Admin sign in Label
areAdmin = Label(root, text="Administrator sign in", font=("Arial", 18))
areAdmin.pack()
#password label and password
passwordLabel = Label(root, text="Password: ", font=("Arial", 12))
passwordLabel.place(x=300, y=30)
#password entry
adminPasswordEntry = Entry(root)
adminPasswordEntry.place(x=385, y=32.5)
#function for button
def getEnteredPassword():
enteredPassword = adminPasswordEntry.get()
if enteredPassword == password:
admin.deiconify()
else:
wrong.deiconify()
#enter button for password
passwordEnterButton = Button(root, text="Enter", width=20, command=getEnteredPassword)
passwordEnterButton.place(x=335, y=60)
mainloop()
Upvotes: 2
Views: 1090
Reputation: 140168
I don't know tkinter
much but I could fix your code, I hope it's a proper fix.
Toplevel
windows not Tk
. those are dialog windows, as opposed to Tk
window which must be unique. Same look & feel, same methodsFixed code, enter wrong or good password as many times you want without crash:
import tkinter
from tkinter import *
password="good"
#define root window
root = tkinter.Tk()
root.minsize(width=800, height = 600)
root.maxsize(width=800, height = 600)
#Admin sign in Label
areAdmin = Label(root, text="Administrator sign in", font=("Arial", 18))
areAdmin.pack()
#password label and password
passwordLabel = Label(root, text="Password: ", font=("Arial", 12))
passwordLabel.place(x=300, y=30)
#password entry
adminPasswordEntry = Entry(root)
adminPasswordEntry.place(x=385, y=32.5)
#function for button
def getEnteredPassword():
enteredPassword = adminPasswordEntry.get()
if enteredPassword == password:
admin = tkinter.Toplevel()
admin.minsize(width=800, height = 600)
admin.maxsize(width=800, height = 600)
#admin.withdraw()
else:
wrong = tkinter.Toplevel()
wrong.minsize(width=200, height = 100)
wrong.maxsize(width=200, height = 100)
Label(wrong, text="Sorry that password is incorrect!", font=("Arial", 24), anchor=W, wraplength=180, fg="red").pack()
#enter button for password
passwordEnterButton = Button(root, text="Enter", width=20, command=getEnteredPassword)
passwordEnterButton.place(x=335, y=60)
mainloop()
Upvotes: 3