Last active
February 10, 2022 04:20
-
-
Save Jithender5913/ec2b3db49a73aaf15742d1b89f31ab20 to your computer and use it in GitHub Desktop.
Password Manager GUI App using Python Tkinter, List comprehension, JSON data and Exception Handling
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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_letter = [choice(letters) for _ in range(randint(8, 10))] | |
password_symbol = [choice(symbols) for _ in range(randint(2, 4))] | |
password_number = [choice(numbers) for _ in range(randint(2, 4))] | |
password_list = password_letter + password_number + password_symbol | |
shuffle(password_list) | |
password_final = "".join(password_list) | |
password_entry.insert(0, password_final) | |
pyperclip.copy(password_final) | |
# ---------------------------- SAVE PASSWORD ------------------------------- # | |
def save(): | |
website = website_entry.get() | |
email = email_entry.get() | |
password_length = password_entry.get() | |
new_data = { | |
website: { | |
"email": email, | |
"password": password_length, | |
} | |
} | |
if len(website) == 0 or len(password_length) == 0: | |
messagebox.showwarning(title="Oops", message="Please enter the concerned fields") | |
else: | |
messagebox.askokcancel(title=website, message=f"These are the details entered \nEmail:{email}\n"f"Password:" | |
f"{password_length}\n""Is it okay to save?") | |
try: | |
with open("data.json", mode="r") as data_file: | |
# Reading old data | |
data = json.load(data_file) # how to read JSON Data | |
except FileNotFoundError: | |
with open("data.json", "w") as data_file: | |
json.dump(new_data, data_file, indent=4) # how to write JSON Data | |
else: | |
# Updating old data with new data | |
data.update(new_data) # Saving updated data | |
with open("data.json", mode="w") as data_file: | |
json.dump(data, data_file, indent=4) | |
finally: | |
website_entry.delete(0, END) | |
password_entry.delete(0, END) | |
# ---------------------------- FIND PASSWORD ------------------------------- # | |
def find_password(): | |
website = website_entry.get() | |
try: | |
with open("data.json", mode="r") 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}\n password: {password}") | |
else: | |
messagebox.showinfo(title="Error", message=f"No details found for {website}") | |
# ---------------------------- UI SETUP ------------------------------- # | |
window = Tk() | |
window.title("Password Manager GUI") | |
window.config(padx=50, pady=50, bg="white") | |
canvas = Canvas(width=200, height=200, bg="white", highlightthickness=0) | |
lock_image = PhotoImage(file="logo.png") | |
canvas.create_image(100, 100, image=lock_image) | |
canvas.grid(column=1, row=0) | |
website_entry = Entry(width=35) | |
website_entry.grid(column=1, row=1, columnspan=2) | |
website_entry.focus() # cursor will jump straight into the entry box | |
website_label = Label(text="Website", bg="white", fg="black") | |
website_label.grid(column=0, row=1) | |
email_entry = Entry(width=20) | |
email_entry.grid(column=1, row=2) | |
email_entry.insert(0, "sayie@gmail.com") | |
email_label = Label(text="Email/Username:", bg="white", fg="black") | |
email_label.grid(column=0, row=2) | |
password_entry = Entry(width=21) | |
password_entry.grid(column=1, row=3) | |
password_label = Label(text="Password:", bg="white", fg="black") | |
password_label.grid(column=0, row=3) | |
password_generate_button = Button(text="Generate password", bg="white", fg="black", command=generate_password) | |
password_generate_button.grid(column=2, row=3) | |
add_button = Button(text="Add", width=36, bg="white", fg="black", command=save) | |
add_button.grid(column=1, row=4, columnspan=2) | |
search_button = Button(text="Search", width=13, bg="white", fg="black", command=find_password) | |
search_button.grid(column=3, row=1) | |
window.mainloop() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment