Python

Password Generator

With the continuous advent of Technology and the great amount of data floating around, the security data is of prime importance today.

This is done by the usage of passwords which keep our data safe and secure from the world and the internet.

However, an easy password can be hacked very easily, and our data can become vulnerable to misuse. To prevent this from happening, it is extremely important to use strong passwords.

In this article, we will learn to create a ‘PASSWORD GENERATOR’ in python using GUI to generate strong passwords using Python Tkinter Module. This application will generate a random password: with a combination of letters, numerical values, and some special characters. The user will mention the length of the password based on the requirement and accordingly, we will get our password.

By the end of this article, you will get a clear understanding of the working of the application and the logic behind the code.

Creating a  GUI ‘PASSWORD GENERATOR’ Using Python Language

Importing Tkinter Library and starter Code

Import the Tkinter and random library as shown below.

Create an instance of Tkinter’s window, Tk(). Set the title of the window as ’Password Generator’. Now set the dimensions of the window to 500 x 300 pixels using the Geometry method.

The Code Block

Ask the users how long they wants password to be and take input from the user

Create a Label Frame that holds the text: “HOW MANY CHARACTERS?”. Name this ‘lf’. Add small padding along the y-axis using the pack function.

Create an Entry box to designate the number of characters, chosen by the user. For this, we use the function ‘Entry’ and pass lf (as we want the entry box to be in the label frame). Set the font style to Helvetica, and size to 24.

Name this entry box as ‘my_entry’.

Add a little padding to this too along both the axis so that we get a spaced box to enter a number of characters.

The Code Block

Entry Box For Displaying Our Generated Password

We could have just flashed our final returned password on the screen using a Label but then we can’t drag and copy it. Therefore, we use an entry frame. This entry frame will be able to contain as many characters as the user wants in his /her password.

Create an Entry box named ‘pw_entry’. Pass the root, an empty text, font style as Helvetica, and size as 24. Set the order to 0 and the background color to ‘system button face’. This is the default system color, and the entry box will now get invisible and look like a label instead.

Add a little padding here too along the y axis using the pack function.

The Code Block

Create a Frame and 2 buttons: ‘Generate Password’ and ‘Copy to Clipboard’

Create a frame name my_frame for our buttons. Add padding along the y-axis. Next, Create ‘my_button’ using the Button function with arguments: my_frame(the frame that will contain the button), text = “Generate Strong Password” and command= new_rand( function to generate password, which will be defined in the next step). Grid this with row=0, column=0, and padding along the axis for spacing between the two buttons.

The next button is to copy the password to the clipboard. Create ‘clip_button’ using the Button function with arguments: my_frame(the frame that will contain the button), text = “Copy to Clipboard” and command= clipper ( function to copy, which will be defined in the next step). Grid this too with row=0, column=1, and padding along the axis for spacing between the two buttons.

End the code with root. mainloop().

The Code Block

Defining new_rand() and clipper()

new_rand():

First, we clear the entry box of all the previous data (previously generated password) using delete( 0, END).

Next, we need to get the password length from the number entered by the user in Step 2 stored in the variable my_entry. So we initialize pw_length to my_entry.get(). Further, typecast the same to integer/ int as we want the length of the password to be a proper integer number (no decimals).

Create a variable to hold our password, here we use ‘my_password’. We set it empty initially. Next, we use a loop and we generate all the characters randomly upto the pw_length and append them together.

Also, to generate random characters we use the ASCII code which assigns each character a particular number (0-127). You can refer to the ASCII Table sheet too. Note that the characters corresponding to 0-32 and 127 cannot be used in the password hence are of no use to us. So, we generate a random number in the range of 33-126 and then convert it to an ASCII character using the ‘chr’ function.

We store the result in ‘my_password’, as shown below.

To generate the output of the above-obtained password in the entry box ‘ pw_entry’, we use the insert function and pass 0( position) and my_password as the arguments.

We can now run the code and you will get the final password. You can copy it and use it now. However, it would be great to create a ‘Copy To Clipboard’ button separately.

Clipper():

We first clear the clipboard of previous data using the clipboard_clear() function.

Next, we just copy data in ‘pw_entry’ i.e. the generated password to the clipboard using the clipboard_append() function and pass pw_entry as an argument in it.

The Code Block

Conclusion

You too can develop this useful app, ‘ PASSWORD GENERATOR’ using Python GUI. This helps you build on your coding skills and using python as the language makes it much easier to code and comprehend as well.

Please note that this post is divided into parts for the legibility of the code and your easy understanding.

About the author

Shalini Salotra