Coders Packet

Password generator in Python using Tkinter

By Sudipta Ghosh

The project aims to generate a random password with a specified length in Python using the Tkinter module.

In this tutorial, we will be looking at how to generate a strong password for greater security purposes in Python. So, to build the project we will use 1.Tkinter- a standard GUI library.
2.The random-It can generate random numbers.
3.string- It contains several functions which can be used as a letter of the password.


To install the libraries we can do the following steps:

pip install tkinter
pip install pyperclip
pip install random
pip install strings

After installing the libraries the following steps are required to generate a random password:-
first, we need to import the libraries-

from tkinter import *
import random,string

next, we will initialize the window-

base=Tk()
base.geometry("400x300")
base.title(" password generator in Python")

Tk()=windows created
geometry()=width and height
title()=set the title of the created window

 

After initializing the window we will work on the strength of the password:-

title=StringVar()
label=Label(base,textvariable=title).pack()
title.set("strength of the password")

def selection():
    selection=choice.get()

choice=IntVar()
T1=Radiobutton(base,text='POOR',variable=choice,value=1,command=selection).pack(anchor=CENTER)
T2=Radiobutton(base,text='AVERAGE',variable=choice,value=2,command=selection).pack(anchor=CENTER)
T3=Radiobutton(base,text='ADANCED',variable=choice,value=3,command=selection).pack(anchor=CENTER)
labelchoice=Label(base)
labelchoice.pack()

Here, I divided the password strength into three parts: poor; average; advanced. If we want to make a weak password we will click on the poor button and average password then we will click on the average button. If the password has to an advanced one then we will choose the advanced option.

Then we will give the range of the length:-

lengthlabel=StringVar()
lengthlabel.set("password length")
lengthtitle=Label(base,textvariable=lengthlabel).pack()

value=IntVar()
spinlength=Spinbox(base,from_=9,to_=32,textvariable=value,width=13).pack()



In the above code, I have given the range of the length of the password between 9 to 32. So, if we choose 10, then the length of the password will be 10.

Then, I have created a function: generator to create ASCII value according to the strength of the password. The code is given below:

def generator():
    isumm.config(text=passgen())
passgenButton=Button(base,text='generate password',bd=5,height=2,command=generator,pady=3)
passgenButton.pack()
password=str(generator)
    
isumm=Label(base,text="")
isumm.pack(side=BOTTOM)
    
poor=string.ascii_uppercase+string.ascii_lowercase
average=string.ascii_uppercase+string.ascii_lowercase+string.digits
sym="`[email protected]#%^&*(){}[];:,.?/<>/"
advanced=poor+average+sym

The final step:

Here a function is created where the option of choice(1:poor;2:average;3:advanced) is given.

def passgen():
    if choice.get()==1:
        return"".join(random.sample(poor,val.get()))
    if choice.get()==2:
        return"".join(random.sample(average,val.get()))
    if choice.get()==3:
        return"".join(random.sample(advanced,val.get()))
  

base.mainloop()

With these steps, we can successfully create the password generator using Python.

 

 

 

Download project

Reviews Report

Submitted by Sudipta Ghosh (Sudipta)

Download packets of source code on Coders Packet