Site icon DataFlair

Python Password Generator

python password generator

Python course with 57 real-time projects - Learn Python

Learn how to create a random password generator in Python.

We know that passwords are a real security threat. To keep your account safe and prevent your password from being hacked you have to make your password hard enough that nobody can guess.

Password Generator

It is a tool that generates passwords based on the given guidelines that you set to create an unpredictable strong password for your accounts.

The Password generator tool creates a random and customized password for users that helps them to create a strong password which provides greater security.

Password Generator Python Project

 

The objective of this project is to create a password generator using python. The password generator project will be build using python modules like Tkinter, random, string, pyperclip.

In this project, the user has to select the password length and then click on the “Generate Password” button. It will show the generated password below. If the user clicks on the “Copy To Clipboard” button, then it will copy the password automatically.

Project Prerequisites

To build this project we will use the basic concept of python and libraries – Tkinter, pyperclip, random, string.

To install the libraries we can use pip installer from the command line:

pip install tkinter
pip install pyperclip
pip install random
pip install strings

Download Project Code

Technology is evolving rapidly!
Stay updated with DataFlair on WhatsApp!!

Download the source code of the password generator project: Python Password Generator

Project File Structure

Let’s check the step to build a Password Generator using Python

Steps to create random password generator

1. Import Libraries

The first step is to import libraries

from tkinter import *
import random, string
import pyperclip

2. Initialize Window

root = Tk()
root.geometry("400x400")
root.resizable(0,0)
root.title("DataFlair - PASSWORD GENERATOR")

 

Label(root, text = 'PASSWORD GENERATOR' , font ='arial 15 bold').pack()
Label(root, text ='DataFlair', font ='arial 15 bold').pack(side = BOTTOM)

Label() widget use to display one or more than one line of text that users can’t able to modify.

3. 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()

4. Function to Generate Password

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)

We have done this because we want a password which must contain an uppercase, a lowercase, a digit, and a special symbol.

Now the password is set to the pass_str() variable.

Button(root, text = "GENERATE PASSWORD" , command = Generator ).pack(pady= 5)

Entry(root , textvariable = pass_str).pack()

5. Function to Copy Password

def Copy_password():
    pyperclip.copy(pass_str.get())

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

pyperclip.copy() used to copy the text to clipboard

Python Password Generator Output

Summary

With these steps, we have successfully created a random password generator project using python. We used popular tkinter library to rendering graphics in our display window and we also learned about pyperclip and random library.

We learned how to create buttons, input textfield, labels, and spinbox. In this way, we successfully created our password generator python project. Hope you enjoyed it.

Exit mobile version