Creating a Local Password Manager Using Python and Tkinter
King Mhar Bayato
Reliability Engineer @ Etihad | AMOS | Data Engineer | SQL | System Analyst
Managing passwords for various accounts can be daunting in today's digital age. This challenge led me to follow a tutorial using a local password manager using Python and Tkinter, with guidance from an online tutorial on Udemy's Python Bootcamp.
Setting Up the GUI
The first step was to create a graphical user interface (GUI) using Tkinter. Tkinter is a standard GUI library for Python, making it straightforward to create windows, labels, entries, and buttons. Here's a snippet of the basic setup:
from tkinter import *
from tkinter import messagebox
window = Tk()
window.title("Local Password Manager")
window.config(padx=20, pady=20)
canvas = Canvas(height=200, width=200)
logo_image = PhotoImage(file="logo.png")
canvas.create_image(100, 100, image=logo_image)
canvas.grid(row=0, column=1)
website_label = Label(text="Website")
website_label.grid(row=1, column=0)
email_label = Label(text="Email/Username")
email_label.grid(row=2, column=0)
password_label = Label(text="Password")
password_label.grid(row=3, column=0)
website_entry = Entry(width=25)
website_entry.grid(row=1, column=1)
website_entry.focus()
email_entry = Entry(width=35)
email_entry.grid(row=2, column=1, columnspan=2)
email_entry.insert(0, "[email protected]")
password_entry = Entry(width=25)
password_entry.grid(row=3, column=1)
search_button = Button(text="Search", width=15, command=find_password)
search_button.grid(row=1, column=2, columnspan=2)
generate_button = Button(text="Generate Password", command=generate_password)
generate_button.grid(row=3, column=2)
add_button = Button(text="Add", width=36, command=save)
add_button.grid(row=4, column=1, columnspan=2)
window.mainloop()
Creating the Password Generator
Next, I implemented a function to generate strong, random passwords. This function uses the random module to create a mix of letters, numbers, and symbols:
领英推荐
import random
import pyperclip
def generate_password():
letters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
numbers = '0123456789'
symbols = '!#$%&()*+'
nr_letters = random.randint(8, 10)
nr_symbols = random.randint(2, 4)
nr_numbers = random.randint(2, 4)
password_letters = [random.choice(letters) for _ in range(nr_letters)]
password_symbols = [random.choice(symbols) for _ in range(nr_symbols)]
password_numbers = [random.choice(numbers) for _ in range(nr_numbers)]
password_list = password_letters + password_symbols + password_numbers
random.shuffle(password_list)
password = "".join(password_list)
password_entry.insert(0, password)
pyperclip.copy(password)
Saving Passwords Securely
To store the passwords securely, I used JSON for file handling. This involved creating a function to save passwords into a JSON file:
import json
def save():
website = website_entry.get()
email = email_entry.get()
password = password_entry.get()
new_data = {
website: {
"email": email,
"password": password,
}
}
if len(website) == 0 or len(password) == 0:
messagebox.showerror(title="Oooopppsss", message="Don't leave the website/password 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)
Retrieving Saved Passwords
Finally, I added functionality to search for saved passwords by the website name. This function reads from the JSON file and displays the relevant information:
def find_password():
website = website_entry.get()
try:
with open("data.json") as data_file:
data = json.load(data_file)
except FileNotFoundError:
messagebox.showinfo(title="Error", message="No data file found")
else:
if website in data:
email = data[website]["email"]
password = data[website]["password"]
messagebox.showinfo(title=website, message=f"Email: {email}\nPassword: {password}")
else:
messagebox.showinfo(title="Website not found", message="No details for the website exist.")
Conclusion
By following the comprehensive steps outlined in the Udemy Python Bootcamp, I successfully created a local password manager using Python and Tkinter. This project enhanced my coding skills and provided a practical tool to manage passwords securely. The process involved setting up a GUI, generating strong passwords, securely saving them, and implementing a search feature—all invaluable learning experiences.
Aeronautical Engineer, Stroke Survivor, Author, Chef, World Traveller. I am a Miracle in the Desert
7 个月Well Done Boss. I use another GUI module than Thinker and I find it easier to use. I will check and let you know. I can't recall its name now