Create a Random Password Generator using Python

In a recent report, it’s shown that over 80% of breaches related to hacking are a result of stolen or weak passwords. Creating strong and secure passwords is an important step to protect our personal information. Let’s start developing this amazing project that helps in generating random passwords and also learn some concepts of python. So here is the Python password Generator project.

About Random Password Generator

This is a very simple project in which we just have to enter the length of the password and select the password strength from the given options. After entering these two things a password will be generated randomly according to our requirement at the bottom of the screen.

Python Password Generator Project

The objective of this project is to generate passwords in Python. It requires three modules: Tkinter , random and string. Knowledge of functions in python is recommended in this project.

Project Prerequisites

Good knowledge of Tkinter is required along with the knowledge of functions in Python for this project.

Download Password Generator

Please download the source code of python password generator: Python Password Generator Project Code

Project File Structure

Steps to follow to create a password Generator in Python:

1. Importing Libraries
2. Initializing Window
3. Program Title
4. Create Radio Buttons
5. Length of Password
6. Logic of the Program
7. Creating a button to Generate Password

Let’s start the password generator project in Python:

1. Importing Libraries

To start with the project the first step is to import libraries.

import string,random
from tkinter import *

Code Explanation:

a. tkinter = It is the most commonly used technique to create Graphical User Interface.
b. random = This module generates random numbers.
c. string = This module helps to manipulate strings with the help of additional tools.

2. Initializing project window

Screen= Tk()
Screen.title("password Generator by ProjectGurukul")

Code Explanation:

a. Tk(): It’s the standard interface in python.
b. geometry(): Size, Position, and other attributes of the layout of the screen are decided by this function.
c. title(): The first letter of every word in the string is converted to capital letter with the help of this function.

3. Program Title

Title = StringVar()
TitleLabel = Label(Screen, textvariable=Title).pack()
Title.set("Random Password generator in Python - ProjectGurukul")

Code Explanation:

a. StringVar(): It holds a string.
b. Label(): It is used to display text, images and even bitmaps.
c. set(): Values of the variable can be set using this method.

4. Create Radio Buttons

def SelectionOptions():
    SelectionOptions = Choice.get()
Choice= IntVar()
RadioButton1 = Radiobutton(Screen,text="POOR",variable=Choice,value=1,command=SelectionOptions).pack(anchor=CENTER)
RadioButton2 = Radiobutton(Screen,text="AVERAGE",variable=Choice,value=2,command=SelectionOptions).pack(anchor=CENTER)
RadioButton3 = Radiobutton(Screen,text="STRONG",variable=Choice,value=3,command=SelectionOptions).pack(anchor=CENTER)
LabelChoice = Label(Screen)
LengthLabel = StringVar()
LengthLabel.set("Password Length")
LengthTitle = Label(Screen,textvariable=LengthLabel).pack()

Code Explanation:

Choice variable contains an integer value. SelectionOptions function made to create radio buttons.

a. IntVar(): It holds a variable.
b. Radiobutton: This widget provides multiple choice to the user.
c. pack(): The position of widgets is declared by this method in Relation to one another.
d. anchor: It is used to define the relative position of text.

5. Length of Password:

Value = IntVar()
SpinLength = Spinbox(Screen, from_=9, to_=25 , textvariable = Value , width = 14).pack()

Code Explanation:

a. Spinbox(): This widget is used to select the length of the password.

From 9 to 25 denotes the length of the password that can be set.

b. textvariable = Current text is retrieved to the entry widget.

6. Logic Of The Program:

Poor = string.ascii_uppercase+string.ascii_lowercase
Average = string.ascii_uppercase+string.ascii_lowercase+string.digits
Symbols="""~`! @#$%^&*()_-+={[}]|\:;"'<,>.?/ """
Advance= Poor+Average+Symbols
def PassGen():
    if Choice.get()==1:
        return "".join(random.sample(Poor,Value.get()))
    if Choice.get()==2:
        return "".join(random.sample(Average,Value.get()))
    if Choice.get()==3:
        return "".join(random.sample(Advance,Value.get()))

Code Explanation:

Poor variable is set such that the password generated contains a password that is made up of lower and uppercase letters. Average variable generates a password containing lowercase, uppercase letters, and digits. The Advanced variable generates a password that contains symbols, lowercase, uppercase letters and digits.

a. string.ascii_uppercase: It consists of all the uppercase letters.

b. string.ascii_lowercase: It consists of all the lowercase letters.

c. string.digits: It consists of all the digits.

d. get(): It helps in getting the text written inside the text widget.

e. join(): It is used to concatenate the string.

f. random.sample(): It is used to generate random passwords.

7. Creating a button to generate password:

def CallBack():
PassGenButton = Button(Screen,text="generate Password",bd=5, height=2 ,command=CallBack,pady=3)

Code Explanation:

PasswordGen variable generates a button. Lsum variable takes the password generated from the function CallBack() and displays it at the bottom of the screen.

a. config(): It is used to change the property of a widget.
b. bd: The size of the border is represented with the help of this option.
c. pady: It represents the number of pixels that are needed to pad widget.
d. mainloop(): It runs the event loop in tkinter.

Python Password Generator Output

python password generator output


We have completed the password Generator python project successfully. We used three python modules: tkinter, string, and random, they are required to start developing the project. Few functions are also created to develop this python project.

Leave a Reply

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