TIL Python Basics Day 29 - Password Manager GUI App

이다연·2021년 1월 12일
0

Udemy Python Course

목록 보기
29/64

Building a Password Manager GUI App with Tkinter

Purpose: Store password information in a local txt file. Generate random password using Tkinter.

Auto entry input & delete
0 or END: (Tkinter Constant) cursor is shown the very first/ last character inside entry
#prepopulated with most frequently used email by giving a starting value

password_input.insert(0, password)
website_input.delete(0, END)
password_input.delete(0, END) #delete range

Layout
columnspan value gives how many columns it will span across.

website_input.grid(column=1, row=1, columnspan=2) 

Copy the password into clipboard

import pyperclip
    pyperclip.copy(password)
from tkinter import *
from tkinter import messagebox
import random
import pyperclip
# ---------------------------- 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 = ['!', '#', '$', '%', '&', '(', ')', '*', '+']

    nr_letters = random.randint(8, 10)
    nr_symbols = random.randint(2, 4)
    nr_numbers = random.randint(2, 4)

    password_letters = []

    password_letters = [random.choice(letters) for char in range(nr_letters)]
    password_letters += [random.choice(symbols) for char in range(nr_symbols)]
    password_letters += [random.choice(numbers) for char in range(nr_numbers)]

    random.shuffle(password_letters)

    password = "".join(password_letters)

    password_input.insert(0, password)

    pyperclip.copy(password)

    #print(f"Your password is: {password}")

# ---------------------------- SAVE PASSWORD ------------------------------- #

def save():
    website = website_input.get()
    email = email_input.get()
    password = password_input.get()

    check_website = len(website)
    check_password = len(password)

    if check_website ==0 or  check_password == 0:
        messagebox.showwarning(title="Oops", message="Please make sure you haven't left any fields empty.")

    else:
        is_ok = messagebox.askokcancel(title=website, message=f"These are the details entered: \nEmail: {email}"
                                                              f"\nPassword: {password}\n Is it ok to save?")
        if is_ok:

            with open("data.txt", "a") as data:
                data.write(f"{website} | {email} | {password} \n")
            website_input.delete(0, END)
            password_input.delete(0, END)



# ---------------------------- UI SETUP ------------------------------- #

window = Tk()
window.title("Password Manager")
window.config(padx=50, pady=50)

# title = Label(text="Timer", font=(FONT_NAME, 32, "bold"), fg=GREEN, bg=YELLOW)
# title.grid(column=1,row=0)

canvas = Canvas(width=200, height=200)
vault_img = PhotoImage(file="logo.png")
canvas.create_image(100, 100, image=vault_img)
canvas.grid(column=1,row=0)


#labels
website = Label(text="Website: ")
website.grid(column=0,row=1)
email = Label(text="Email/Username: ")
email.grid(column=0, row=2)
password = Label(text="Password: ")
password.grid(column=0, row=3)

#entries
website_input = Entry(width=38)
website_input.grid(column=1, row=1, columnspan=2) #how many columns it will span
website_input.focus() #cursor ready


email_input = Entry(width=38)
email_input.grid(column=1, row=2, columnspan=2)
email_input.insert(0, "dummy@email.com")  #0 or END: Tkinter Constant: cursor is shown the very first/ last character inside entry
#prepopulated with most frequently used email by giving a starting value

password_input = Entry(width=24)
password_input.grid(column=1, row=3)


#button
password_button = Button(text="Generate Password", command=generate_password)
password_button.grid(column=2, row=3)
add_button = Button(text="Add", width=38, command=save)
add_button.grid(column=1, row=4, columnspan=2)




window.mainloop()
profile
Dayeon Lee | Django & Python Web Developer

2개의 댓글

comment-user-thumbnail
2023년 9월 21일

Are these solutions good for Mac systems?
Still thanks for sharing!

답글 달기
comment-user-thumbnail
2023년 9월 21일

It seems to me that this is a universal solution, but there are much cooler ready-made solutions for macOS.

답글 달기