Password Generator GUI using Python

Introduction

The Password Generator project is a simple yet powerful tool that allows users to create strong, random passwords for securing their online accounts and data. This project utilizes Python’s tkinter library to create a graphical user interface (GUI) and employs the random, string, and pyperclip libraries for password generation and clipboard functionality.

Prerequisites

  • Basic understanding of Python programming.
  • Familiarity with loops, conditional statements, and user input.
  • An understanding of Python libraries such as tkinter, random, and string.

Learning Objectives

By completing this project, you will learn:

  • How to create a GUI application using tkinter
  • How to generate random passwords with various character types
  • How to interact with the clipboard using the pyperclip library
  • Basic project structuring and documentation skills

Installation Guide

To run this project on your machine, ensure you have Python installed. Follow these steps:

  1. Download and Install Python: Visit the official Python website to download and install Python if you haven’t already.
  2. Set Up a Python Environment: Use a code editor like Visual Studio Code or an IDE like PyCharm to write and run your code.
  3. Clone or Copy the Code: Copy the project code provided below into a new Python file in your development environment.
  4. Install Required Libraries: Open your terminal or command prompt and run the following command to install the necessary libraries:

    pip install pyperclip

Project Steps

  1. Import Libraries: Import the necessary libraries, including tkinter, random, string, and pyperclip.
  2. Initialize the Window: Create the main application window using tkinter.
  3. Create GUI Elements: Add labels, buttons, and input fields to the GUI.
  4. Define the Password Generator Function: Write a function to generate random passwords.
  5. Copy to Clipboard Functionality: Implement a function to copy the generated password to the clipboard.
  6. Run the Main Loop: Start the GUI application.

Code

				
					#importing Libraries
from tkinter import *
import random, string  
import pyperclip

### initialize window
root =Tk()
root.geometry("400x400")
root.resizable(0,0)
root.title("edSlash Coding Hub - PASSWORD GENERATOR")

# heading
heading = Label(root, text = 'PASSWORD GENERATOR' , font ='arial 15 bold').pack()
Label(root, text ='edSlash Coding Hub', font ='arial 15 bold').pack(side = BOTTOM)

### select password length
pass_label = Label(root, text = 'PASSWORD LENGTH', font = 'arial 10 bold').pack()
pass_len = IntVar()
length = Spinbox(root, from_ = 8, to_ = 32 , textvariable = pass_len , width = 15).pack()

##### define function
pass_str = StringVar()

def Generator():
    password = ''
    for x in range (0,4):
        password = random.choice(string.ascii_uppercase)+random.choice(string.ascii_lowercase)+random.choice(string.digits)+random.choice(string.punctuation)
    for y in range(pass_len.get()- 4):
        password = password+random.choice(string.ascii_uppercase + string.ascii_lowercase + string.digits + string.punctuation)
    pass_str.set(password)
    
### button
Button(root, text = "GENERATE PASSWORD" , command = Generator ).pack(pady= 5)
Entry(root , textvariable = pass_str).pack()

######## function to copy
def Copy_password():
    pyperclip.copy(pass_str.get())

Button(root, text = 'COPY TO CLIPBOARD', command = Copy_password).pack(pady=5)

# loop to run program
root.mainloop()
				
			

Code

				
					#importing Libraries
from tkinter import *
import random, string  
import pyperclip

### initialize window
root =Tk()
root.geometry("400x400")
root.resizable(0,0)
root.title("edSlash Coding Hub - PASSWORD GENERATOR")

# heading
heading = Label(root, text = 'PASSWORD GENERATOR' , font ='arial 15 bold').pack()
Label(root, text ='edSlash Coding Hub', font ='arial 15 bold').pack(side = BOTTOM)

### select password length
pass_label = Label(root, text = 'PASSWORD LENGTH', font = 'arial 10 bold').pack()
pass_len = IntVar()
length = Spinbox(root, from_ = 8, to_ = 32 , textvariable = pass_len , width = 15).pack()

##### define function
pass_str = StringVar()

def Generator():
    password = ''
    for x in range (0,4):
        password = random.choice(string.ascii_uppercase)+random.choice(string.ascii_lowercase)+random.choice(string.digits)+random.choice(string.punctuation)
    for y in range(pass_len.get()- 4):
        password = password+random.choice(string.ascii_uppercase + string.ascii_lowercase + string.digits + string.punctuation)
    pass_str.set(password)
    
### button
Button(root, text = "GENERATE PASSWORD" , command = Generator ).pack(pady= 5)
Entry(root , textvariable = pass_str).pack()

######## function to copy
def Copy_password():
    pyperclip.copy(pass_str.get())

Button(root, text = 'COPY TO CLIPBOARD', command = Copy_password).pack(pady=5)

# loop to run program
root.mainloop()
				
			

Pseudo Code explaining this Python Project

				
					1.	Import Required Libraries:
o	Import tkinter, random, string, and pyperclip.
2.	Initialize the Application Window:
o	Create a window using Tk() and set its dimensions.
3.	Add GUI Elements:
o	Add a label for the heading and another for the subheading.
o	Create a Spinbox to select the password length.
o	Create an Entry widget to display the generated password.
4.	Define the Password Generator Function:
o	Initialize an empty string for the password.
o	Use a loop to add random characters from different categories (uppercase, lowercase, digits, punctuation) to ensure diversity.
o	Fill the remaining characters with a random selection from all categories.
5.	Define the Clipboard Copy Function:
o	Use pyperclip to copy the generated password to the clipboard.
6.	Create Buttons:
o	Add a button to trigger the password generation.
o	Add another button to copy the password to the clipboard.
7.	Start the Main Loop:
o	Use mainloop() to run the application.

				
			

Pseudo Code explaining this Python Project

				
					1.	Import Required Libraries:
o	Import tkinter, random, string, and pyperclip.
2.	Initialize the Application Window:
o	Create a window using Tk() and set its dimensions.
3.	Add GUI Elements:
o	Add a label for the heading and another for the subheading.
o	Create a Spinbox to select the password length.
o	Create an Entry widget to display the generated password.
4.	Define the Password Generator Function:
o	Initialize an empty string for the password.
o	Use a loop to add random characters from different categories (uppercase, lowercase, digits, punctuation) to ensure diversity.
o	Fill the remaining characters with a random selection from all categories.
5.	Define the Clipboard Copy Function:
o	Use pyperclip to copy the generated password to the clipboard.
6.	Create Buttons:
o	Add a button to trigger the password generation.
o	Add another button to copy the password to the clipboard.
7.	Start the Main Loop:
o	Use mainloop() to run the application.

				
			

Flow Chart

Code Explanation

  • Window Initialization: The Tk() function initializes the main window. The geometry method sets the window size, and resizable(0, 0) makes it non-resizable.
  • Labels and Spinbox: Labels are used for displaying text, and a Spinbox allows users to select the password length.
  • Password Generation Logic: The Generator function creates a password using random selections from uppercase letters, lowercase letters, digits, and punctuation. The first loop ensures at least one of each type is included, and the second loop fills in the rest of the password length.
  • Copy Function: The Copy_password function copies the generated password to the clipboard using pyperclip.copy.
  • GUI Elements: Buttons are created using the Button widget to trigger actions, and an Entry widget displays the password.

Challenges and Solutions

  • Challenge: Ensuring the password contains a mix of character types.
    • Solution: Use separate random selections for uppercase, lowercase, digits, and punctuation.
  • Challenge: Handling dynamic password length.
    • Solution: Use the Spinbox to allow users to select the desired length.

Testing

  1. Run the Python script.
  2. Select a password length using the spinbox.
  3. Click “GENERATE PASSWORD” to create a password.
  4. Verify the password contains a mix of characters.
  5. Click “COPY TO CLIPBOARD” and paste the password elsewhere to ensure it was copied.

Additional Resources

FAQs

Q: What is the purpose of using pyperclip?

A: pyperclip allows the program to interact with the system clipboard, making it easy to copy and paste the generated password.

Q: Can I customize the password length?

A: Yes, the Spinbox widget allows users to select a password length between 8 and 32 characters.

Q: How can I modify the character set used for password generation?

A: You can adjust the character sets by modifying the strings in the Generator function, such as string.ascii_uppercase or string.digits.

Project by Nimisha Agrawal and Documented by Aakarsh Pandey, Team edSlash.