PythonProjects [1/50] - GUI가 있는 암호 생성기

27894 단어 python

블로그 001 - 비밀번호 생성기 Python



tkinter를 사용하여 GUI로 비밀번호 생성기를 만드는 간단한 안내서

GitHub의 프로젝트: https://github.com/zsoltszakal/python-random-password-tkinter

1단계: 사용자 인터페이스 프로토타입 생성



나는 www.figma.com에 사용

2단계: tkinter로 사용자 인터페이스 만들기



##########     User Interface     ##########
window = Tk()
window.title("Password Generator")
window.config(padx=50, pady=50, bg="#383e56")

label_title = Label(text="Password Generator",
                    bg="#383e56",
                    fg="#c5d7bd",
                    font=("Arial", 35, "bold"))
label_title.grid(row=0, column=0, columnspan=3, pady=30)

label_before_input = Label(text="I want a password with",
                           bg="#383e56",
                           fg="#c5d7bd",
                           font=("Arial", 15, "bold"))
label_before_input.grid(row=1, column=0)

char_input = Entry(bg="#fb743e")
char_input.grid(row=1, column=1)

label_after_input = Label(text="characters.",
                          bg="#383e56",
                          fg="#c5d7bd",
                          font=("Arial", 15, "bold"))
label_after_input.grid(row=1, column=2)

generate_password_button = Button(text="Generate Password & Copy to Clipboard",
                                  bg="#fb743e",
                                  height=4,
                                  width=55,
                                  command=password_generator)
generate_password_button.grid(row=2, column=0, columnspan=3, padx=50, pady=50)

password_field = Entry(bg="#383e56",
                       font=("Arial", 15, "bold"))
password_field.grid(row=3, column=0, columnspan=3, , width=40)

window.mainloop()

3단계 - 비밀번호 생성기 구축



from tkinter import *
import string
import random

##########     Password Generator     ##########
password_chars = string.ascii_letters + string.digits + string.punctuation

def password_generator():
    length = 10
    password = "".join([random.choice(password_chars) for _ in range(length)])

4단계 - 버튼에 암호 생성기 명령 추가



##########     User Interface - add command=password_generator   ##########
generate_password_button = Button(text="Generate Password & Copy to Clipboard",
                                  bg="#fb743e",
                                  height=4,
                                  width=55,
                                  command=password_generator)

5단계 - 사용자 입력을 사용하여 비밀번호 길이 결정



##########     Password Generator - Get the value for lenght from char_input entry    ##########
password_chars = string.ascii_letters + string.digits + string.punctuation

def password_generator():
    length **= int(char_input.get())**
    password = "".join([random.choice(password_chars) for _ in range(length)])

##########     User Interface - add .focues to export value for entry   ##########

char_input = Entry(text="Hello",
                   bg="#fb743e")
char_input.grid(row=1, column=1)
char_input.insert(0, "12")
char_input.focus()

6단계 - GUI에 비밀번호 표시



##########     Password Generator     ##########
### add .insert to show password
### add .delete to remove the last password

password_chars = string.ascii_letters + string.digits + string.punctuation

def password_generator():
    password_field.delete(0, END)
    length = int(char_input.get())
    password = "".join([random.choice(password_chars) for _ in range(length)])
    password_field.insert(0, password)

7단계 - 클립보드에 복사



import pyperclip 

##########     Password Generator     ##########
### import pyperclip and then add pyperclip.copy to copy password to clipboard

password_chars = string.ascii_letters + string.digits + string.punctuation

def password_generator():
    password_field.delete(0, END)
    length = int(char_input.get())
    password = "".join([random.choice(password_chars) for _ in range(length)])
    password_field.insert(0, password)
    pyperclip.copy(password)

전체 코드:

from tkinter import *
import string
import random
import pyperclip

##########     Password Generator     ##########
password_chars = string.ascii_letters + string.digits + string.punctuation

def password_generator():
    password_field.delete(0, END)
    length = int(char_input.get())
    password = "".join([random.choice(password_chars) for _ in range(length)])
    password_field.insert(0, password)
    pyperclip.copy(password)

##########     User Interface     ##########
window = Tk()
window.title("Password Generator")
window.config(padx=50, pady=50, bg="#383e56")

label_title = Label(text="Password Generator",
                    bg="#383e56",
                    fg="#c5d7bd",
                    font=("Arial", 35, "bold"))
label_title.grid(row=0, column=0, columnspan=3, pady=30)

label_before_input = Label(text="I want a password with",
                           bg="#383e56",
                           fg="#c5d7bd",
                           font=("Arial", 15, "bold"))
label_before_input.grid(row=1, column=0)

char_input = Entry(bg="#fb743e")
char_input.grid(row=1, column=1)
char_input.insert(0, "12")
char_input.focus()

label_after_input = Label(text="characters.",
                          bg="#383e56",
                          fg="#c5d7bd",
                          font=("Arial", 15, "bold"))
label_after_input.grid(row=1, column=2)

generate_password_button = Button(text="Generate Password & Copy to Clipboard",
                                  bg="#fb743e",
                                  height=4,
                                  width=55,
                                  command=password_generator)
generate_password_button.grid(row=2, column=0, columnspan=3, padx=50, pady=50)

password_field = Entry(bg="#383e56",
                       font=("Arial", 15, "bold"), width=40)
password_field.grid(row=3, column=0, columnspan=3)

window.mainloop()


이제 암호 생성기가 준비되었습니다.


Github의 프로젝트: https://github.com/zsoltszakal/python-random-password-tkinter

좋은 웹페이지 즐겨찾기