How to create a strong password generator using Python

How to create a strong password generator using Python

Create a strong password generator using python with the Tkinter library

Create a strong password generator using python with the Tkinter library

strong password generator.png

If you are a Python newbie developer and want to learn python fast and fun way, then you are in right place. Today I will guide you on how can you create a strong password generator mini project using python and Tkinter library.

Prerequisite

  1. Basic knowledge of python and Tkinter library
  2. Willing to learn and motivation

Let's start, first create a Graphical User Interface(GUI) window to display the generated password and button to generate them.

Write import statement and bare-bone code

import random
from Tkinter import *
from string import ascii_letters, digits, punctuation


root = Tk()
root.title('Strong Password Generator - Yourname')
root.geometry('700x500')
root.configure(background='#0bb53b')


 # Body of the program


root.mainloop()

Create heading Label to display heading

#Create a Label for heading
heading_label = Label(root, text='Strong Password Generator', font="cursive 26 bold", anchor=CENTER, bg='#0bb53b')
heading_label.pack(pady=50)

A Slider to set how many digits of a strong password to generate

#Create slider to set number digits
digit_scale = Scale(root, from_=5, to=32, orient=HORIZONTAL, width=50,  font='courier 30', activebackground='#0fdfff', bd=1, highlightcolor='orange', length=360, relief=SUNKEN, sliderlength=70, troughcolor='#0bb53b', bg='#a12298', fg='white')
digit_scale.pack()

Create button to generate a random number

# Create button to generate random number
generate_button = Button(root, text="Generate", command=diaplay_password, font="Cursive 20 bold", bg='#ff0fef', fg='white')
generate_button.pack(pady=40)

It's time to create a function that performs some operations in order to generate some random string, punctuations, and numbers.

write down the below function after root.configure(background='#0bb53b')

Create a function to display the password on the screen

display_password = Label(root)

#Create a function to display password in screen
def diaplay_password():
    global display_password
    display_password.destroy()
    password_length = digit_scale.get()

    # We join them with an empty string so the sequence becomes a string
    random_password = ''.join(random.choices(ascii_letters + digits + punctuation, k=password_length))    

    display_password = Label(root, text= "Password: " + random_password, font="Helvetica 20 bold", bg='#0bb53b', fg='#40007d')
    display_password.pack()

We are done. Congratulation.

See how your final project looks like:

import random
from tkinter import *
from string import ascii_letters, digits, punctuation


root = Tk()
root.title('Strong Password Generator - Yourname')
root.geometry('700x500')
root.configure(background='#0bb53b')

display_password = Label(root)

#Create a function to display password in screen
def diaplay_password():
    global display_password
    display_password.destroy()
    password_length = digit_scale.get()

    # We join them with an empty string so the sequence becomes a string
    random_password = ''.join(random.choices(ascii_letters + digits + punctuation, k=password_length))    

    display_password = Label(root, text= "Password: " + random_password, font="Helvetica 20 bold", bg='#0bb53b', fg='#40007d')
    display_password.pack()



#Create a Label for heading
heading_label = Label(root, text='Strong Password Generator', font="cursive 26 bold", anchor=CENTER, bg='#0bb53b')
heading_label.pack(pady=50)

#Create slider to set number digits
digit_scale = Scale(root, from_=5, to=32, orient=HORIZONTAL, width=50,  font='courier 30', activebackground='#0fdfff', bd=1, highlightcolor='orange', length=360, relief=SUNKEN, sliderlength=70, troughcolor='#0bb53b', bg='#a12298', fg='white')
digit_scale.pack()

# Create button to generate random number
generate_button = Button(root, text="Generate", command=diaplay_password, font="Cursive 20 bold", bg='#ff0fef', fg='white')
generate_button.pack(pady=40)

root.mainloop()

If you found any mistakes or logical errors then feel free to reach out to me at TWITTER

Any feedback would be appreciated.

sailendra signature.png