password generator in python using gui

introduction
In the digital era, the importance of strong, secure passwords cannot be overstated. With the ever-growing number of online accounts, users often struggle to create and remember unique passwords for each platform. Weak or reused passwords are a common cause of security breaches. To address this issue, this project presents an intuitive and powerful Password Generator in Python with a clean and simple graphical user interface (GUI) powered by the tkinter
module.
The Password Generator enables users to generate complex passwords that include a mix of uppercase letters, lowercase letters, digits, and special characters. It gives users control over the password length and character composition, enhancing both security and customization. With the addition of a Copy to Clipboard feature, users can conveniently use the generated password anywhere with just one click.
This project is implemented using Jupyter Notebook, which allows for easy interactive development and experimentation, making it ideal for learning and demonstrating core Python concepts like functions, conditionals, loops, GUI programming, and string manipulation.
Key Highlights:
-
Password length selection input.
-
Checkboxes to choose character sets (uppercase, lowercase, digits, symbols).
-
One-click password generation.
-
Copy-to-clipboard functionality.
-
Built with simple Python and
tkinter
GUI for ease of use.
Why Use a Password Generator in python?
-
Humans tend to create weak, predictable passwords.
-
Manual generation is time-consuming and error-prone.
-
Automated tools like this one ensure randomness and strength.
-
It promotes better password hygiene and reduces the risk of hacking.
Objectives of the Project
-
To generate strong and secure passwords based on user preferences.
-
To provide a clean and simple GUI using Python’s
tkinter
module. -
To allow easy copying of generated passwords using clipboard functionality.
-
To help users manage digital security effectively with minimal effort.
Technologies Used
-
Python 3.x – Programming Language
-
Tkinter – GUI Toolkit
-
String & Random Modules – For password logic
-
Jupyter Notebook – Development Environment
This Password Generator project is an excellent beginner-level application for learning how to build interactive Python applications with GUI, and also a practical tool to improve your own digital security.
steps to create password generator in python
Step 1: Open Jupyter Notebook
Launch Anaconda Navigator or run
jupyter notebook
in terminal/command prompt.Create a new Python 3 Notebook.
Step 2: Import Required Libraries
import tkinter as tk
from tkinter import messagebox
import random
import string
These libraries help with GUI creation (tkinter
) and character selection (random
, string
).
Step 3: Define Password Generation Logic
def generate_password():
try:
length = int(length_entry.get())
if length < 4:
messagebox.showwarning("Invalid Length", "Password length should be at least 4")
return
characters = ""
if use_upper.get():
characters += string.ascii_uppercase
if use_lower.get():
characters += string.ascii_lowercase
if use_digits.get():
characters += string.digits
if use_symbols.get():
characters += string.punctuation
if not characters:
messagebox.showwarning("Selection Error", "Please select at least one character set!")
return
password = ''.join(random.choice(characters) for _ in range(length))
password_var.set(password)
except ValueError:
messagebox.showerror("Input Error", "Enter a valid number for length!")
Step 4: Define Clipboard Copy Function
def copy_to_clipboard():
root.clipboard_clear()
root.clipboard_append(password_var.get())
messagebox.showinfo("Copied", "Password copied to clipboard!")
Step 5: Create GUI Window and Components
# GUI Window
root = tk.Tk()
root.title("🔐 Password Generator")
root.geometry("400x300")
root.config(bg="lightgray")
# Title
tk.Label(root, text="Password Generator", font=("Helvetica", 16, "bold"), bg="lightgray").pack(pady=10)
# Password Length
tk.Label(root, text="Enter Password Length:", bg="lightgray").pack()
length_entry = tk.Entry(root)
length_entry.pack()
# Checkbuttons
use_upper = tk.BooleanVar(value=True)
use_lower = tk.BooleanVar(value=True)
use_digits = tk.BooleanVar(value=True)
use_symbols = tk.BooleanVar(value=True)
tk.Checkbutton(root, text="Include Uppercase", variable=use_upper, bg="lightgray").pack()
tk.Checkbutton(root, text="Include Lowercase", variable=use_lower, bg="lightgray").pack()
tk.Checkbutton(root, text="Include Digits", variable=use_digits, bg="lightgray").pack()
tk.Checkbutton(root, text="Include Symbols", variable=use_symbols, bg="lightgray").pack()
# Output
password_var = tk.StringVar()
tk.Entry(root, textvariable=password_var, font=("Courier", 12), justify="center").pack(pady=10)
# Buttons
tk.Button(root, text="Generate Password", command=generate_password, bg="green", fg="white").pack(pady=5)
tk.Button(root, text="Copy to Clipboard", command=copy_to_clipboard, bg="blue", fg="white").pack()
root.mainloop()
Step 6: Run the Notebook
Execute all cells in the notebook.
A GUI window will appear where you can interact with the password generator.
code explanation
1.import tkinter as tk
Imports the Tkinter library to create the GUI (Graphical User Interface).
tk
is an alias used for shorthand.
2.from tkinter import messagebox
Allows us to display popup messages (like warnings or info alerts).
3.import random, string
random
: Used to randomly select characters for password.string
: Provides predefined character sets like letters, digits, punctuation.
1.Function: generate_password()
def generate_password():
...
This is the main logic that creates the password.
🔸 length = int(length_entry.get())
Retrieves the password length from the input box and converts it to an integer.
🔸 if length < 4:
Ensures the password is at least 4 characters long.
2. Character Set Selection
characters = ""
if use_upper.get(): characters += string.ascii_uppercase
if use_lower.get(): characters += string.ascii_lowercase
if use_digits.get(): characters += string.digits
if use_symbols.get(): characters += string.punctuation
Based on checkboxes, adds selected character types to the pool.
3. Password Generation
password = ''.join(random.choice(characters) for _ in range(length))
password_var.set(password)
Randomly selects characters and combines them into a password.
Updates the GUI with the result.
4.Function: copy_to_clipboard()
def copy_to_clipboard():
root.clipboard_clear()
root.clipboard_append(password_var.get())
messagebox.showinfo("Copied", "Password copied to clipboard!")
Clears the system clipboard and copies the generated password.
Displays a popup saying “Copied”.
5.GUI Window Setup
root = tk.Tk()
root.title("🔐 Password Generator")
root.geometry("400x300")
root.config(bg="lightgray")
Creates the main GUI window.
Sets title, size, and background color.
6.Labels, Entries, and Checkbuttons
tk.Label(...).pack()
length_entry = tk.Entry(root)
...
use_upper = tk.BooleanVar(value=True)
tk.Checkbutton(...).pack()
These elements let the user input password length and select character types.
BooleanVar
is used to track the checkbox state (True/False).
7.Password Display and Buttons
password_var = tk.StringVar()
tk.Entry(root, textvariable=password_var).pack()
tk.Button(root, text="Generate Password", command=generate_password).pack()
tk.Button(root, text="Copy to Clipboard", command=copy_to_clipboard).pack()
Displays the generated password.
Buttons for generating and copying the password.
8.Start the GUI Loop
root.mainloop()
Launches the GUI and waits for user interactions (clicks, inputs).
9.Summary
Component | Purpose |
---|---|
tkinter | Builds the user interface |
string module | Provides sets of characters for password generation |
random module | Picks characters randomly from selected sets |
messagebox | Shows pop-up alerts or warnings |
BooleanVar | Stores True/False for each checkbox |
StringVar | Used to dynamically update password output in entry box |
source code
import tkinter as tk
from tkinter import messagebox
import random
import string
def generate_password():
try:
length = int(length_entry.get())
if length < 4:
messagebox.showwarning("Invalid Length", "Password length should be at least 4")
return
characters = ""
if use_upper.get():
characters += string.ascii_uppercase
if use_lower.get():
characters += string.ascii_lowercase
if use_digits.get():
characters += string.digits
if use_symbols.get():
characters += string.punctuation
if not characters:
messagebox.showwarning("Selection Error", "Please select at least one character set!")
return
password = ''.join(random.choice(characters) for _ in range(length))
password_var.set(password)
except ValueError:
messagebox.showerror("Input Error", "Enter a valid number for length!")
def copy_to_clipboard():
root.clipboard_clear()
root.clipboard_append(password_var.get())
messagebox.showinfo("Copied", "Password copied to clipboard!")
# GUI Window
root = tk.Tk()
root.title("🔐 Password Generator")
root.geometry("400x300")
root.config(bg="lightgray")
# Title
tk.Label(root, text="Password Generator", font=("Helvetica", 16, "bold"), bg="lightgray").pack(pady=10)
# Password Length
tk.Label(root, text="Enter Password Length:", bg="lightgray").pack()
length_entry = tk.Entry(root)
length_entry.pack()
# Checkbuttons for character sets
use_upper = tk.BooleanVar(value=True)
use_lower = tk.BooleanVar(value=True)
use_digits = tk.BooleanVar(value=True)
use_symbols = tk.BooleanVar(value=True)
tk.Checkbutton(root, text="Include Uppercase", variable=use_upper, bg="lightgray").pack()
tk.Checkbutton(root, text="Include Lowercase", variable=use_lower, bg="lightgray").pack()
tk.Checkbutton(root, text="Include Digits", variable=use_digits, bg="lightgray").pack()
tk.Checkbutton(root, text="Include Symbols", variable=use_symbols, bg="lightgray").pack()
# Output Password
password_var = tk.StringVar()
tk.Entry(root, textvariable=password_var, font=("Courier", 12), justify="center").pack(pady=10)
# Buttons
tk.Button(root, text="Generate Password", command=generate_password, bg="green", fg="white").pack(pady=5)
tk.Button(root, text="Copy to Clipboard", command=copy_to_clipboard, bg="blue", fg="white").pack()
root.mainloop()
output

