gpt4 book ai didi

python - 如何允许多次打开 tkinter 窗口

转载 作者:太空宇宙 更新时间:2023-11-04 00:46:19 25 4
gpt4 key购买 nike

我正在用 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()

最佳答案

我不太了解 tkinter 但我可以修复你的代码,我希望它是一个正确的修复。

  1. 创建 Toplevel 窗口而不是 Tk。这些是对话窗口,与必须唯一的 Tk 窗口相对。相同的外观和感觉,相同的方法
  2. 每次都在需要时创建窗口。否则,使用关闭小工具关闭它们会破坏它们。

固定密码,多次输入错误或正确的密码而不会崩溃:

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()

关于python - 如何允许多次打开 tkinter 窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39458318/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com