How to Build a Password Generator Using Python?

Amrutha K 31 Oct, 2023 • 6 min read

Introduction

In this article, we will see how to build the password generator. Password generate is a python application that will generate the random string of the desired length. Nowadays we are using many applications and websites that require passwords. Setting strong passwords is very important to avoid any attack by attackers and to keep our information safe. Now we will build this application using python, Tkinter, and pyperclip.

This article was published as a part of the Data Science Blogathon.

Requirements to Build a Password Generator Using Python

Let us see the requirements that are needed for us to build this application.

  • Python: Python is the programming language that we will use to build the application.
  • Tkinter: Tkinter is a Graphical User Interface library. using Tkinter is one of the easiest ways to build any GUI-based applications. In this application, we use Tkinter to build the window where we generate a random password.
  • pyperclip: Pyperclip is a module in python that is used for copying and pasting the text. So in our application after generating the password we will also have an option to copy the password.
  • Random: Passwords are generated randomly so to generate these passwords randomly we use the random module. This random module generates the random numbers.
  • Strings: The string module in python helps in creating and customizing strings.

Now let us move into the implementation part.

Python Implementation

For any project, we have to start by importing the required modules. For our application, we will import Tkinter, Pyperclip, Random, and strings.

If these libraries are not preinstalled, then you have to install them and then you have to import them. For installing these libraries you have to use pip install to install them. I basically use jupyter notebook to run the code so I open the anaconda prompt and run these commands to install the libraries. You can use any prompt to install them.

  • To install Tkinter
pip install tkinter
  • To install pyperclip
pip install pyperclip
  • To install random
pip install random
  • To install strings
pip install strings

Now import all the libraries. From Tkinter import all the libraries. So to import everything that has in that particular module we use *.

from tkinter import *
import random, string
import pyperclip

Initialize Window

Our next step is to initialize the window where we generate the password by giving the number of digits in the password. For this we use Tkinter. First, we initialize the win variable with Tk() function. Using the geometry function we will set the width and height of the window. and using the title function we will pass the title of the window. Here we set it to “PASSWORD GENERATOR” with height as 500 and width as 500. Here using configure method I set the background color of the window.

win = Tk()
win.geometry("500x500")
win.title("PASSWORD GENERATOR")
win.configure(bg="#ffc252")

At the top of the window, the text is placed saying PASSWORD GENERATOR in bold letters with ariel font and font size 18 and with some background color. Here we use the Pack() function to arrange the widgets in the window.

Label(win, text = 'PASSWORD GENERATOR' , font ='ariel 15 bold',bg="#ffc252").pack()

Now we have to place an input box where the user can input the number of digits the password should contain. Before that, we place the text, “PASSWORD LENGTH” with Arial font and font size 10 with bold letters. Using IntVar() function, we can set integer data as this function holds integer data, and later we can retrieve the data.  Spinbox() provides a range of values for the user to input. Here, users can enter the digits or scroll the numbers and select the length of the password. And here it generates passwords from lengths 8 to 32.

Python Code:

The text and the spinbox will look like this.

Password Generator
Password Generator 2

Define Password Generator

Coming to StringVar() function, it is also similar to the IntVar() function but here stringVar() function holds string data. Now we define a function called Generator which generates random passwords. Firstly the password is initialized with an empty string. Setting a password that contains only numerical digits or with only alphabets doesn’t provide enough security for your system or any application. For any password, it should be a combination of uppercase letters, lower case letters, numerical digits, and some punctuations. For the first four digits of the password, we set it to, a random uppercase letter, random lowercase letter, random digit, and random punctuation. And remaining values will be the random combination of uppercase, lowercase, digits, and punctuations.

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)

Generate Buttons

Now create a button where it follows the Generator command and the Generator is the function that we defined for generating the password. The button contains the text “GENERATE PASSWORD” on it with blue background and white foreground. These buttons are very user-friendly with a nice GUI. You don’t need to stick to the UI that was defined in this article. You can change some text fonts, colors, and many more and you can make your window more beautiful. And you can play around with this and can make your window according to your expectations.

Generate=Button(win, text = "GENERATE PASSWORD" , command = Generator,padx=5,pady=5 )
Generate.configure(background="blue", foreground='white',font=('ariel',10,'bold'))
Generate.pack(side=TOP,pady=20)
Entry(win , textvariable = pass_str).pack()

The generate password button will look like this

Password Generator

Our next step is to copy the password. We use pyperclip to copy the password. Get the string in the pass_str function and then copy it using pyperclip and we create a button which follows the command copy_password with the text “COPY TO CLIPBOARD” button next we configure the button that means how the button should look like. The button contains blue color background and white color foreground and we use ariel font with font size 10 and bold letters. Here we use the pack() function to organize the widgets according to the size of the frame And we set some top pady here.

def Copy_password():
    pyperclip.copy(pass_str.get())
copy=Button(win, text = 'COPY TO CLIPBOARD', command = Copy_password)
copy.configure(background="blue", foreground='white',font=('ariel',10,'bold'))
copy.pack(side=TOP,pady=20)

The copy to the clipboard button will look like this.

Password Generator 2

Final Execution

Now after completion of writing code for all, we have to execute the main loop to run the application. Finally, the window looks like this. We can see it contains the title, “PASSWORD GENERATOR” and at the top, we have “PASSWORD LENGTH” and below there is a spinbox where the user can select the number of digits the password should contain. It can vary from 8 to 32. By default, it will be 8 digits. After that user should click on the ” GENERATE PASSWORD” button and then the password will appear on the empty box below. WHEN you click on the “COPY TO CLIPBOARD” button, the generated password is copied to your clipboard for future purposes.

Final Execution

Now run the main loop to execute the entire application.

win.mainloop()

Here you can see I created the password of 8 digits and the password that I got is ” Sh8_90Ny”. This is a user-friendly application and a very useful application.

Conclusion

Password Generator is an interesting, exciting, and thrilling application. We can use a secret password generator for building strong passwords and this password generator doesn’t store any password anywhere. I clears all the data as soon as you left the window. So without any hesitation, you can build your secret and strong passwords using this password generator.

In this article, overall we have seen,

  • How to build a secret password generator
  • How to use the Tkinter GUI interface
  • Generating and copying the password to the clipboard
  • Working with application

 

The media shown in this article is not owned by Analytics Vidhya and is used at the Author’s discretion.

Amrutha K 31 Oct 2023

Frequently Asked Questions

Lorem ipsum dolor sit amet, consectetur adipiscing elit,

Responses From Readers

Clear

Exadime LLC
Exadime LLC 15 Jul, 2022

Great Post. I am continuously searching online for articles that can benefit me. Definitely I bookmarking your website for revisiting. Thank you once again!!

Exadime LLC
Exadime LLC 15 Jul, 2022

Awesome Post. I am continuously searching online for this type of articles that can benefit me. Definitely I bookmarking your website for revisiting. Thank you once again!!

Exadime LLC
Exadime LLC 15 Jul, 2022

Keep up the great work, I read few blog posts on this web site and I believe that your website is rattling interesting and contains bands of fantastic information.

Related Courses