In the world that demands high confidentiality and security, having a weak password is not acceptable. It turns out that people find it difficult to make up a strong password that is strong enough to prevent unauthorized users.
So, Let’s create a simple application which can randomly generate strong passwords using the Python Tkinter module (Step-by-Step tutorial).
Modules required – ◾ Tkinter ◾ Pyperclip ◾ Random
So, First thing first install all these modules. You can easily download these using PIP. Go to the command prompt and give the commands pip install tkinter and pip install pyperclip (Pyperclip module helps to copy the text to the clipboard). The random module comes in default.
Make sure you have PIP installed. If you don’t have PIP installed on your system you have to install PIP first, but if you are having Python 3.4 or higher ,then it is already there in your PC.
Step 2: Importing Modules
import random
import pyperclip
from tkinter import *
Step 3: Creating the GUI tk window
my_root = Tk()
my_root.title("Random Password Generator")
my_root.geometry("300x300")
my_root.config(bg="Black")
# Write the code here
my_root.mainloop()

Step 4: Add Labels , Entry box and Buttons
l1 = Label(text = "Password Generator", bg = "Black", fg= "Yellow", relief = SUNKEN, borderwidth = 3)
l1.config(font=("Arial",20,"bold"))
l1.pack(pady= 15)
l2 = Label(text = "Enter the no of character", bg = "Black", fg= "Yellow")
l2.config(font=("Arial",10,"bold italic"))
l2.pack()
e1 = Entry()
e1.pack()
b1 = Button(text = "Generate Password", fg ="Black", bg= "Yellow")
b1.pack(side = BOTTOM, pady = 20)

Step 5: Assigning the Functions
def copy2board(x):
pyperclip.copy(x)
#Copy to clipboard
def gen_pass():
password = ""
n_el = int(e1.get())
for i in range(n_el):
if i==int(n_el/2):
password += "@"
else:
password = password + chr(random.randrange(50,122))
l3 = Label(text = str(password) , bg = "Black", fg= "Yellow")
l3.config(font=("Arial",10,"bold italic"))
l3.pack(pady = 10)
b2 = Button(text = "Copy",command = copy2board(password), fg ="Black", bg= "Yellow")
b2.config(width=5)
b2.pack(side = BOTTOM)
'''
Assign the gen_pass() function to 'Generate Password' button
b1 = Button(text = "Generate Password",command =gen_pass, fg ="Black", bg= "Yellow")
'''
Step 5: Final Code –
#Random Password Generator
# Author-RajdeepDutta
import random
import pyperclip
from tkinter import *
my_root = Tk()
my_root.title("Random Password Generator")
my_root.geometry("300x300")
my_root.config(bg="Black")
l1 = Label(text = "Password Generator", bg = "Black", fg= "Yellow", relief = SUNKEN, borderwidth = 3)
l1.config(font=("Arial",20,"bold"))
l1.pack(pady= 15)
l2 = Label(text = "Enter the no of character", bg = "Black", fg= "Yellow")
l2.config(font=("Arial",10,"bold italic"))
l2.pack()
e1 = Entry()
e1.pack()
def copy2board(x):
pyperclip.copy(x)
def gen_pass():
password = ""
n_el = int(e1.get())
for i in range(n_el):
if i==int(n_el/2):
password += "@"
else:
password = password + chr(random.randrange(50,122))
l3 = Label(text = str(password) , bg = "Black", fg= "Yellow")
l3.config(font=("Arial",10,"bold italic"))
l3.pack(pady = 10)
b2 = Button(text = "Copy",command = copy2board(password), fg ="Black", bg= "Yellow")
b2.config(width=5)
b2.pack(side = BOTTOM)
b1 = Button(text = "Generate Password",command =gen_pass, fg ="Black", bg= "Yellow")
b1.pack(side = BOTTOM, pady = 20)
my_root.mainloop()
Final Result : –

