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

dauxcl2d  于 2023-11-20  发布在  Python
关注(0)|答案(1)|浏览(112)

希望你有一个伟大的一天。我创建了一个输入密码界面。输入正确的密码后,它应该显示在标签中授予的访问权限,这与我的代码很好。然而,在不正确的密码的情况下,我想只包括3次尝试。如果你输入不正确的密码第一次和第二次,它应该显示“不正确的密码”。在最后一次尝试后,它应该显示“帐户被阻止”.问题是,当涉及到不正确的密码和第一次尝试,它直接显示“帐户被阻止”这是我到目前为止,

  1. from tkinter import *
  2. root = Tk()
  3. root.geometry("300x300")
  4. # Creating Button Command
  5. def password():
  6. correct_password = "123"
  7. enter_the_password = pass_entry.get()
  8. number_of_try=0
  9. number_of_max_try=3
  10. max_try=""
  11. while enter_the_password!=correct_password and max_try!="reached":
  12. if number_of_try<number_of_max_try:
  13. enter_the_password
  14. number_of_try=number_of_try+1
  15. if enter_the_password!=correct_password:
  16. response= "Incorrect Password"
  17. else:
  18. max_try="reached"
  19. if max_try=="reached":
  20. response= "Too many attempts. Account blocked"
  21. else:
  22. response= "Access Granted"
  23. pass_label.config(text=response)
  24. pass_entry.delete(0, END)
  25. # Creating widget
  26. pass_entry=Entry(root, width=30)
  27. pass_entry.pack(pady=5)
  28. done_button= Button(root, text="Press", command=password).pack(pady=10)
  29. pass_label= Label(root, width=30)
  30. pass_label.pack(pady=10)
  31. root.mainloop()

字符串

g6ll5ycj

g6ll5ycj1#

您的while循环立即重新进入,而不允许用户输入另一个密码。实际上,您不需要该循环,因为root.mainloop()已经提供了它。
相反,你应该删除你的while循环,并在你的函数password之外初始化你的变量。每次按下done_button,函数password应该只检查一次密码。

相关问题