PythonProjects [1/50] - Password Generator with GUI
Tags: python
Published: 2021-03-13 15:46:09

# Blog 001 - Password Generator Python

A simple guide to create a password generator with GUI using tkinter

Project on GitHub:  [https://github.com/zsoltszakal/python-random-password-tkinter](https://github.com/zsoltszakal/python-random-password-tkinter)

## Step 1: Protopy a the user interface

I used [www.figma.com](http://www.figma.com) to 
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zqqvif7lbs3pxpzexniz.png)

## Step 2: Create user interface with tkinter

```python
##########     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()
```

## Step 3 - Build a password generator

```python
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)])
```

## Step 4 - Add password generator command to button

```python
##########     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)
```

## Step 5 - Use user input to determine password length

```python
##########     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()
```

## Step 6 - Show password in GUI

```python
##########     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)
```

## Step 7 - Copy to clipboard

```python
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)
```

Full Code:

```python
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()
```

And the password generator now is ready:
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/m1lrm8ugqs47ag15xl6a.png)

Project on Github: [https://github.com/zsoltszakal/python-random-password-tkinter](https://github.com/zsoltszakal/python-random-password-tkinter)