而使用tkinter在python GUI中循环(几次尝试后阻止密码)



希望你今天过得愉快。我创建了一个输入密码界面。在输入正确的密码后,它应该在标签中显示授予的访问权限,这对我的代码来说很好。但是,如果密码不正确,我只想包括3次尝试。如果您输入了错误的密码1st&第二次应该显示";错误密码";。在最后的尝试之后,它应该显示";账户被冻结";。问题是当涉及到不正确的密码&第一次尝试,它直接向上显示";账户被冻结";,这是我目前所掌握的

from tkinter import *
root = Tk()
root.geometry("300x300")
# Creating Button Command
def password():
correct_password = "123"
enter_the_password = pass_entry.get()
number_of_try=0
number_of_max_try=3
max_try=""
while enter_the_password!=correct_password and max_try!="reached":
if number_of_try<number_of_max_try:
enter_the_password
number_of_try=number_of_try+1
if enter_the_password!=correct_password:
response= "Incorrect Password"
else:
max_try="reached"
if max_try=="reached":
response= "Too many attempts. Account blocked"
else:
response= "Access Granted"

pass_label.config(text=response)
pass_entry.delete(0, END)

# Creating widget 
pass_entry=Entry(root, width=30)
pass_entry.pack(pady=5)
done_button= Button(root, text="Press", command=password).pack(pady=10)
pass_label= Label(root, width=30)
pass_label.pack(pady=10)
root.mainloop()

欢迎使用StackOverflow。

您的while循环立即重新进入,而不让用户键入另一个密码。事实上,您不需要那个循环,因为root.mainloop()已经提供了它

相反,您应该删除while循环,并在函数password之外初始化变量。每次按下done_button时,功能password应只检查密码一次。

最新更新