python - How to allow a tkinter window to be opened multiple times -
i making makeshift sign in system python. if enter correct password brings new admin window. if enter wrong 1 brings new window says wrong password. if exit out of 1 of windows , try enter password again breaks. tkinter.tclerror: can't invoke "wm" command: application has been destroyed
there way prevent if enters wrong password don't need restart app?
import tkinter 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 password 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 , 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 button def getenteredpassword(): enteredpassword = adminpasswordentry.get() if enteredpassword == password: admin.deiconify() else: wrong.deiconify() #enter button password passwordenterbutton = button(root, text="enter", width=20, command=getenteredpassword) passwordenterbutton.place(x=335, y=60) mainloop()
i don't know tkinter
fix code, hope it's proper fix.
- create
toplevel
windows nottk
. dialog windows, opposedtk
window must unique. same & feel, same methods - create windows when needed, , each time. else, closing them using close gadget destroys them.
fixed code, enter wrong or password many times want without crash:
import tkinter 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 , 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 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 password incorrect!", font=("arial", 24), anchor=w, wraplength=180, fg="red").pack() #enter button password passwordenterbutton = button(root, text="enter", width=20, command=getenteredpassword) passwordenterbutton.place(x=335, y=60) mainloop()
Comments
Post a Comment