
We’ve just finished implementing JSON as the storage mechanism for our password manager, and addressing the FileNotFoundError that occurs if the JSON file is missing/not created yet. Here is the current state of the code.
from tkinter import *
from tkinter import messagebox
from random import choice, shuffle, randint
import pyperclip
import json
# ---------------------------- PASSWORD GENERATOR ------------------------------- #
def generate_password():
letters = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']
numbers = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
symbols = ['!', '#', '$', '%', '&', '(', ')', '*', '+']
password_letters = [choice(letters) for _ in range(randint(8, 10))]
password_numbers = [choice(numbers) for _ in range(randint(2, 4))]
password_symbols = [choice(symbols) for _ in range(randint(2, 4))]
password_list = (password_letters + password_numbers + password_symbols)
shuffle(password_list)
password = "".join(password_list)
password_entry.insert(0, password)
pyperclip.copy(password)
# ---------------------------- SAVE PASSWORD ------------------------------- #
def save():
website = website_entry.get()
email = email_entry.get()
password = password_entry.get()
new_data = {
website: {
"email": email,
"password": password
}
}
if len(password) == 0 or len(website) == 0:
messagebox.showinfo(title="Oops!", message="Please don't leave any fields empty!")
else:
try:
with open("data.json", "r") as data_file:
data = json.load(data_file)
except FileNotFoundError:
with open("data.json", "w") as data_file:
json.dump(new_data, data_file, indent=4)
else:
data.update(new_data)
with open("data.json", "w") as data_file:
json.dump(data, data_file, indent=4)
finally:
website_entry.delete(0, END)
password_entry.delete(0, END)
# ---------------------------- UI SETUP ------------------------------- #
window = Tk()
window.title("Password Manager")
window.config(padx=20, pady=20)
canvas = Canvas(width=200, height=200)
logo_img = PhotoImage(file="logo.png")
canvas.create_image(100,100, image=logo_img)
canvas.grid(row=0, column=1)\\
#Website
website_label = Label(text="Website: ")
website_label.grid(row=1, column=0, sticky="E")
website_entry = Entry(width=35)
website_entry.grid(row=1, column=1, columnspan=2, sticky="EW")
website_entry.focus()
#Email
email_label = Label(text="Email/Username: ")
email_label.grid(row=2, column=0, sticky="E")
email_entry = Entry(width=35)
email_entry.insert(END, "[email protected]")
email_entry.grid(row=2, column=1, columnspan=2, sticky="EW")
#Password
password_label = Label(text="Password: ")
password_label.grid(row=3, column=0, sticky="E")
password_entry = Entry(width=21)
password_entry.grid(row=3, column=1, padx=0, pady=0, sticky="EW")
#Generate Password
gen_pass_button = Button(text="Generate Password", command=generate_password)
gen_pass_button.grid(row=3, column=2, sticky="EW")
#Add Password
add_password_button = Button(text="Add Password", width=36, command=save)
add_password_button.grid(row=4, column=1, columnspan=2, sticky="EW")
window.mainloop()
Now we want to add search functionality and a search button to load the password the user request. There are quite a few small task to accomplish this, but nothing we haven’t seen before. Lets get to it.
find_password() that gets triggered when the “Search” button is presseddata.jsondata.json showing a messagebox with the text “No Data File Found”