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.
Let us see the requirements that are needed for us to build this application.
Now let us move into the implementation part.
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.
pip install tkinter
pip install pyperclip
pip install random
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
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:
from tkinter import *
import random, string
import pyperclip
win = Tk()
win.geometry("500x500")
win.title("PASSWORD GENERATOR")
win.configure(bg="#ffc252")
Label(win, text = 'PASSWORD GENERATOR' , font ='ariel 15 bold',bg="#ffc252").pack()
pass_label = Label(win, text = 'PASSWORD LENGTH', font = 'ariel 10 bold',bg="#ffc252").pack(pady=10)
pass_len = IntVar()
length = Spinbox(win, from_= 8, to_ = 32 , textvariable = pass_len , width = 15).pack()
The text and the spinbox will look like this.
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)
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
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.
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.
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.
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,
The media shown in this article is not owned by Analytics Vidhya and is used at the Author’s discretion.
Great Post. I am continuously searching online for articles that can benefit me. Definitely I bookmarking your website for revisiting. Thank you once again!!
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!!
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.