It is always important to have strong passwords for online websites and accounts, in order to keep your data safe and prevent others from guessing the password or hacking into your account.
That’s why in this article we are going to be creating a program to automatically generate a secure password that will help keep your private information safe. Users will select the number of characters they want their password to be, and the program will create one using upper and lowercase letters, numbers, and symbols. Whether you are new to Python or have some prior experience, this project is a great way to learn more about making functional programs and helps you apply your knowledge in a practical and fun way.
Who is this Project For?
This is an intermediate-level project for those who are new to Python. At this point, you should already know the basics of creating functions, installing packages, and creating simple GUIs (graphical user interfaces) with Tkinter. These are the concepts that you will use and build upon as you code this project.
What Will We Learn?
This project focuses on using the Tkinter and random libraries to create new passwords. Tkinter is used to create the program’s GUI, while the random and string modules are used to randomly select characters to use. This is a relatively simple project that has a wide variety of uses, so let’s get started!
Features to Consider:
- The user should be able to select the number of characters the password is
- The generated passwords must be random, they cannot repeat
- The password should be made up of a combination of every type of character (numbers, symbols, upper and lowercase letters)
This project can be broken down into 4 main steps:
- Import and setup modules
- Create the Tkinter GUI
- Define the Generator function
- Create the GUI’s ‘Generate Password’ button
Step 1: Import the Modules:
The first thing we’ll need to do to begin the project is import all of the libraries and modules that we’ll be using. We will use the Tkinter library for the graphics, and the random and string modules to randomly select characters for the password. First, you’ll need to use the command line to install the packages. To do this, you will type:
Now, we’ll import the libraries we need and initialize the Tkinter window. To do this, we’ll start by using the import command to import the Tkinter, random, and string modules to the project. Next, we’ll initialize the window and set a size, in this case, the window will be 400x200 pixels. Finally, we’ll title the window to make its purpose clear. Once this is complete, the code should look like this:
Step 2: Create the Tkinter GUI
Now that all of the modules have been imported and initialized, it’s time to create the program’s graphical user interface. We’ll start by creating two labels- one to serve as the title, and one to label where users can input their desired password length. To do this, you will type:
Hint: Note that the font, size of text, and message can be adjusted according to how you want the project to look. Make sure that the text is aligned and is in place by using the pack() method.
Next, we need to create the spinbox widget that will allow users to select the number of characters their password should be. This will be done by adding this line of code:
This line of code creates a spinbox widget that allows users to select a length for their password. In this case, we’ve set the parameters to be 8-32, meaning they can select any number between these values.
Step 3: Define the Generator Function
Now that the GUI is mostly complete, it’s time to define the main generator function. We’ll start by setting up a string-type variable to store the password, then we’ll create the function and a variable called “password” which will be set to an empty string. At this point the code should look like this:
Next, we’ll use for-loops to randomly select characters for the password. Start by creating a loop to repeat itself four times. Then, we’ll make the program set the password to a random uppercase letter, lowercase letter, number, and punctuation. Then, using another for-loop, we’ll do the same thing for a range of 0 to the inputted length minus 4. This will ensure that the password follows the specific character sequence and still randomly selects the correct number of characters. Finally, we’ll set the “pass_str” string to make it equal to the password that was just generated.
Hint: This will allow us to display the user’s password in the GUI so that they can view it. An alternate way to do this may be printing the password, but it is not as effective.
The code for this function will be:
Step 4: Create the ‘Generate Password’ Button
Finally, we just need to add the ‘Generate Password’ button, as well as display the password itself on the GUI. To do this, we will start by creating a button to create the password. We will set the button’s command to the ‘Generator’ function so that when the button is pressed the password will be created. Then, we’ll create a new label and entry to show the user’s password. To create the label, use the same format as was used for the title, and the entry will use ‘pass_str’ as its’ text variable. This will display the password in the text box and make it clear for the user to see. Once this step is complete, the code should look like this:
And now the project is complete! At this point, you will be able to use the password generator to create strong and secure passwords that will be hard to guess or hack. Feel free to test your code now and see how it works. If you’re stuck or have any issues with your code, try reviewing it again either in your text editor or by looking at the code mentioned above as a reference.