This is my code:
def masterpassgui(password):
masterpassword = password
add_window = tk.Tk()
add_window.title("Master Password")
add_window.geometry("250x250")
masterpass_label = tk.Label(add_window, text="Enter your master password:")
masterpass_label.grid(row=0, column=0, sticky="w")
masterpass_entry = tk.Entry(add_window)
masterpass_entry.grid(row=0, column=1)
masterpass = masterpass_entry.get()
attempts = 5
def on_click():
messagebox.showwarning(f"You have {attempts} attempts left. After that it will lock down.")
while masterpass != masterpassword:
masterpass_label = tk.Label(add_window, text="Enter your master password:")
masterpass_label.grid(row=0, column=0, sticky="w")
masterpass_entry = tk.Entry(add_window)
masterpass_entry.grid(row=0, column=1)
masterpass = masterpass_entry.get()
if masterpass != masterpassword:
attempts -= 1
if attempts == 0:
sys.exit()
else:
on_click()
else:
break
add_window.mainloop()`
I am trying to make a password manager, but it is getting stuck on this piece of code.
I was expecting for it ask my master password, and if it was wrong, it would give me a warning, saying that you have some amount of attempts left. Instead, it just give me 5 consecutive warning messages, and then shuts down. Why is this happening?
In your code, the
masterpassvariable is not updated inside the loop, so it's always checking against the initial empty string. Additionally, theget()method retrieves the content of the entry widget at the time it is called, not dynamically as the user types.I have modified like: