While Loop in python GUI with tkinter ( blocking password after a few attempts )

Viewed 31

Hope you're having a great day. I created an Input Password interface. After inputting the correct password it should be showing access granted in a Label, which works fine with my code. However, in case of an incorrect password, I want to include only 3 attempts. If you input an incorrect password 1st & the 2nd time, it should be showing "incorrect password". After final attempts, it should be showing "account blocked". The problem is when it comes to incorrect passwords & 1st attempt, It straight up shows that "account blocked" Here is what I've got so far,

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

   
1 Answers

Welcome to StackOverflow.

Your while loop immediately re-enters without letting the user type another password. In fact you do not need that loop because root.mainloop() already provides it.

Instead, you should remove your while loop, and initialize your variables outside your function password. Every time the done_button is pressed, the function password should check the password only once.

Related