Creating a Local Password Manager Using Python and Tkinter

Creating a Local Password Manager Using Python and Tkinter


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.

Marco Giovannoli

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

回复

要查看或添加评论,请登录

King Mhar Bayato的更多文章

  • Creating Automated Birthday Greetings

    Creating Automated Birthday Greetings

    In today’s digital age, automating routine tasks can save time and add a personal touch to interactions. One such task…

    1 条评论
  • Testing the tkinter libraries

    Testing the tkinter libraries

    I'm on day 27 of my online course and just learned about arguments and kwargs. These concepts are beneficial for…

  • Part 1 of Building a Snake Game ( DAY 22 )

    Part 1 of Building a Snake Game ( DAY 22 )

    After an intense 20-day learning streak in the Python BootCamp, I took a much-needed three-week break. I was exhausted,…

  • Day 15, Coffee Project

    Day 15, Coffee Project

    Day 15 of my course, and my final project for the beginner class is to simulate a coffee vending machine. The…

  • Building a Follower Comparison Game in Python

    Building a Follower Comparison Game in Python

    In this tutorial, we'll walk through creating an engaging follower comparison game using Python. The game uses random…

  • Guess the Number

    Guess the Number

    The instructor tested our knowledge by assigning us a task to develop specific programs. Surprisingly, I managed to…

  • The Basic Blackjack or 21 in Python

    The Basic Blackjack or 21 in Python

    In developing a mini blackjack game, I first used the teacher's flowchart to outline the game's logic, ensuring clarity…

  • Day 10 of python boot camp

    Day 10 of python boot camp

    In this Python boot camp session, the main project revolves around creating a program known as the "secret auction…

  • Day 8 of Python Bootcamp

    Day 8 of Python Bootcamp

    We have been tasked with creating a Caesar cipher program for encoding and decoding. This project presents some…

  • Building 'Hangman': A Developer's Experience at an Online Bootcamp

    Building 'Hangman': A Developer's Experience at an Online Bootcamp

    During a seven-day online boot camp, a novice programmer embarked on a journey to create a mini-game called "Hangman."…

其他会员也浏览了