我正在使用python在系统中进行临时签名。目前,如果您输入正确的密码,它会显示一个新的管理窗口。如果输入错误,则会显示一个错误密码的新窗口。如果您退出其中一个窗口,然后尝试再次输入密码,它会中断。 tkinter.TclError: can't invoke "wm" command: application has been destroyed
有没有办法防止这种情况,所以如果有人输入了错误的密码,他们就不需要重启应用了?
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()
答案 0 :(得分:3)
我不太了解tkinter
,但我可以修复您的代码,我希望这是一个正确的解决方法。
Toplevel
窗口而不是Tk
。那些是对话窗口,而不是Tk
窗口,它必须是唯一的。相同的外观&感觉,方法相同修复了代码,输入错误或良好的密码,无需崩溃即可:
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()