Why does the tkinter entry not compare with password

40 Views Asked by At

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?

1

There are 1 best solutions below

0
TheHungryCub On BEST ANSWER

In your code, the masterpass variable is not updated inside the loop, so it's always checking against the initial empty string. Additionally, the get() method retrieves the content of the entry widget at the time it is called, not dynamically as the user types.

I have modified like:

import tkinter as tk
from tkinter import messagebox
import sys

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)
    
    attempts = 5

    def on_click():
        messagebox.showwarning(f"You have {attempts} attempts left. After that, it will lock down.")

    def check_password():
        nonlocal attempts
        masterpass = masterpass_entry.get()
        if masterpass != masterpassword:
            attempts -= 1
            if attempts == 0:
                sys.exit()
            else:
                on_click()
        else:
            add_window.destroy()

    submit_button = tk.Button(add_window, text="Submit", command=check_password)
    submit_button.grid(row=1, columnspan=2)

    add_window.mainloop()

# Example usage
masterpassgui("your_master_password")