Password Generator – Python Source Code

  • Code
  • Code With GUI
# Importing required modules
import string
import random

# Print a title for the password generator
print("\n**Password Generator**")

# Ask the user whether to include special characters
include_special_character = input("Do you want to include special characters? (yes/no): ").lower()

# Validate user input
while include_special_character not in ['yes', 'no']:
    include_special_character = input("Please enter 'yes' or 'no': ").lower()

# Ask the user for the desired password length
while True:
        password_length = int(input("Enter Password Length: "))
        if password_length < 3:
            print("Password Must Be At Least 3 Characters")
    except ValueError:
        print ("Please enter a valid integer for the length of password.")

# Define the set of characters to choose from
characters = string.ascii_letters + string.digits + string.punctuation

# Include special characters if the user wants to
if include_special_character == 'yes':
    characters += string.punctuation

# Generate the password by randomly selecting characters from the defined set
password = ''.join(random.choice(characters) for _ in range(password_length))

# Print the generated password
print ("password :", password)

#Password Generator Source Code - By Codotix
# Importing Required Modules
import string
import random
import tkinter as tk
from tkinter import messagebox
import pyperclip

# Function to generate a password
def generate_password(length, add_unique_char=True):
    characters = string.ascii_letters + string.digits 

    # If add_unique_char is True, include special punctuation characters
    if add_unique_char:
        characters += string.punctuation

    # Generate password using random characters from the defined set
    password = ''.join(random.choice(characters) for _ in range(length))
    return password

# Function to handle the "Generate Password" button
def generate_button():
        # Get password length from the entry field
        password_length = int(pass_length_entry.get())
        # Get the value of the checkbox for unique characters
        add_unique_char = add_unique_char_var.get()

        # Check if the password length is valid
        if password_length < 3:
            messagebox.showerror("Error", "Password length must be at least 3 Characters")

            # Generate a password and update the password label
            password = generate_password(password_length, add_unique_char)
            password_label.delete(0, tk.END)
            password_label.insert(0, password)
        messagebox.showerror("Erro", "Ivalid Input, Please Enter A Number")

# Function to copy the generated password to the clipboard
def copy_clipboard():
    password = password_label.get()

# Function to toggle password visibility
def toggle_password():
    if password_hide_var.get() == 1:
        # Show '*' instead of actual characters
        # Show actual characters

# Create the main Tkinter window
root = tk.Tk()
root.title("Password Generator")

# Create a label for entering password length
pass_length_label = tk.Label(root, text="Enter Password Length:", font="arial 20 bold")

# Create an entry field for password length input
pass_length_entry = tk.Entry(root, font="arial 18")

# Create a Boolean variable for adding unique characters option
add_unique_char_var = tk.BooleanVar()
# Create a checkbox for adding unique characters
add_unique_char_check = tk.Checkbutton(root, text="Add Unique Characters",font="arial 15" ,variable=add_unique_char_var)

# Create a button to generate passwords
password_generate_button = tk.Button(root, text="Generate Password",font="arial 15",bd=3 ,command = generate_button)

# Create an entry field to display the generated password
password_label = tk.Entry(root, show="", font="arial 16")

# Create an integer variable for password visibility toggle
password_hide_var = tk.IntVar()
# Create a checkbox to hide or show password characters
password_hide_check = tk.Checkbutton(root, text="Hide Password", font="arial 15", variable=password_hide_var, command=toggle_password)

# Create a button to copy the password to clipboard
password_copy_button = tk.Button(root, text="Copy To Clipboard",font="arial 15",bd=3, command=copy_clipboard)

# # Start the GUI event loop

# Password Generator Source Code - By Codotix

Leave a Comment

Your email address will not be published. Required fields are marked *