격자 레이아웃 생성하기
데이터를 파일에 저장하기
Tkinter의 대화 박스와 팝업창
- Message Boxes : 가장 널리 사용되는 Standard Dialogs
- 간단히 메시지 박스 모듈을 임포트 할 수 있음
- showinfo, showwarning, showerror, yes or no, OK or Cancel, 재시도나 취소 등
⇨ 우클릭 - go to - implementation으로 어떻게 구현되어 있는지 확인 가능
패스워드 생성하고 클립보드에 복사하기
- 기존 리스트 생성하는 반복문을 컴프리헨션 사용해서 정리
기존코드
변경코드
클립보드에 자동복사
pyperclip 모듈 사용
Final Code
from tkinter import *
from tkinter import messagebox
from random import choice, randint, shuffle
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 = ['!', '#', '$', '%', '&', '(', ')', '*', '+']
password_letters = [choice(letters) for _ in range(randint(8, 10))]
password_symbols = [choice(symbols) for _ in range(randint(2, 4))]
password_numbers = [choice(numbers) for _ in range(randint(2, 4))]
password_list = password_letters + password_symbols + password_numbers
shuffle(password_list)
password = "".join(password_list)
password_entry.insert(0, password)
pyperclip.copy(password)
messagebox.showinfo(title="Message", message="The password has been created and copied.")
# ---------------------------- SAVE PASSWORD ------------------------------- #
def save():
website = website_entry.get()
email = email_entry.get()
password = password_entry.get()
if len(website) == 0 or len(password) == 0:
messagebox.showinfo(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} \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=50, pady=50)
canvas = Canvas(height=200, width=200)
logo_img = PhotoImage(file="logo.png")
canvas.create_image(100, 100, image=logo_img)
canvas.grid(row=0, column=0, columnspan=3) # Center the canvas by spanning 2 columns
# Labels
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)
# Entries
website_entry = Entry(width=36) # Set width to 36
website_entry.grid(row=1, column=1, columnspan=2)
website_entry.focus()
email_entry = Entry(width=36) # Set width to 36
email_entry.grid(row=2, column=1, columnspan=2)
email_entry.insert(0, "dahna0915@gmail.com")
password_entry = Entry(width=21)
password_entry.grid(row=3, column=1)
# Buttons
generate_password_button = Button(text="Generate Password", command=generate_password)
generate_password_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()