
The password generator is almost done. Here is the current state of our code:
from tkinter import *
from tkinter import messagebox
# ---------------------------- PASSWORD GENERATOR ------------------------------- #
# ---------------------------- SAVE PASSWORD ------------------------------- #
def save():
website = website_entry.get()
email = email_entry.get()
password = password_entry.get()
if len(password) == 0 or len(website) == 0:
messagebox.showinfo(title="oop", message="Please don't leave any fields empty!")
else:
is_ok = messagebox.askokcancel(title=website, message=f"These are the details entered:\\nEmail: {email} \\nPassword: {password} \\nIs it ok to save?")
if is_ok:
with open("data.txt", "a") as data_file:
data_file.write(f"{website} | {email} | {password}\\n")
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")
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()
Previously on Day 5 we built a program that generated passwords. Dr. Yu has modified the code a little so we don’t have to use any inputs now. Here’s the code she is providing:
#Password Generator Project
import random
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_list = []
for char in range(nr_letters):
password_list.append(random.choice(letters))
for char in range(nr_symbols):
password_list += random.choice(symbols)
for char in range(nr_numbers):
password_list += random.choice(numbers)
random.shuffle(password_list)
password = ""
for char in password_list:
password += char
print(f"Your password is: {password}")
Let’s copy this into our password manager. When we look at this code we have some for loops we can clean up with list comprehension.
