Python Password Generator Using Tkinter

Published February 07, 2022

With today's technological advancements, cybersecurity has recently become a major worry. Passwords are among the key techniques for securing our data and important information. However, Passwords, on the other hand, do not provide complete protection since they may be guessed or hacked. To improve the privacy of our data, we must use strong passwords.In this python example, we will create a simple Python password generator application using the Tkinter library, which will allow us to swiftly and randomly generate strong passwords.

Here are the simple steps to create a simple Python password generator using Tkinter Library.

Step 1: First,  import the required modules.  To do so, add the following code:

import random

import pyperclip

from tkinter import *

from tkinter.ttk import *

 

Step 2: Let's start by designing our application's UI. We need a password suggestion input form, a password length Combobox, a radio button to pick the strength of the password, and two buttons for copying and generating passwords in our interface.

Paste  the code below

# create GUI window

root = Tk()

var = IntVar()

var1 = IntVar()

root.title("Python Password Generator Using Tkinter")

Random_password = Label(root, text="Suggest Password")

Random_password.grid(row=0)

entry = Entry(root)

entry.grid(row=0, column=1)

c_label = Label(root, text="Password Length")

c_label.grid(row=1)

copy_button = Button(root, text="Copy", command=copy1)

copy_button.grid(row=0, column=2)

generate_button = Button(root, text="Generate", command=generate)

generate_button.grid(row=0, column=3)

radio_low = Radiobutton(root, text="Strong", variable=var, value=1)

radio_low.grid(row=1, column=2, sticky='E')

radio_middle = Radiobutton(root, text="Medium", variable=var, value=0)

radio_middle.grid(row=1, column=3, sticky='E')

radio_strong = Radiobutton(root, text=" Very Strong", variable=var, value=3)

radio_strong.grid(row=1, column=4, sticky='E')

combo = Combobox(root, textvariable=var1)

combo['values'] = (8, 9, 10, 11, 12, 13, 14, 15, 16,

                                                  17, 18, 19, 20, 21, 22, 23, 24, 25,

                                                    26, 27, 28, 29, 30, 31, 32, "Length")

combo.current(0)

combo.bind('<<ComboboxSelected>>')

combo.grid(column=1, row=1)

root.mainloop()

The following will be displayed if you execute the above code; however, no passwords will be generated. In the next step, we will develop functions to provide the functionality to our application.

 

Step 3: Now, we need to create the functions to generate a password, copy generated password to the clipboard, generate strong, medium, and very strong passwords.

Function to create strong, medium and very strong passwords

 

def low():

                entry.delete(0, END)

                length = var1.get()

                lower = "abcdefghijklmnopqrstuvwxyz"

                upper = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"

                digits = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789 !@#$%^&*()"

                password = ""

                # For strong password

                if var.get() == 1:

                            for i in range(0, length):

                                        password = password + random.choice(lower)

                            return password

 

                # For medium password

                elif var.get() == 0:

                            for i in range(0, length):

                                        password = password + random.choice(upper)

                            return password

                # For very strong password

                elif var.get() == 3:

                            for i in range(0, length):

                                        password = password + random.choice(digits)

                            return password

                else:

                            print("Please choose an option")

Function to Generate password

 

def generate():

                password1 = low()

                entry.insert(10, password1)

Function to copy password to Clipboard

 

def copy1():

                random_password = entry.get()

                pyperclip.copy(random_password)

 

Step 4: Let us now launch our application. It works exactly as intended.

Let's generate an 8 character very strong password from the password suggestion "Python."

Python password generator Suggest Password

 

 

Python password Generator Generate Password

 

Conclusion: Python Password Generator example

 

Download Source code

 

Article Contributed By :
https://www.rrtutors.com/site_assets/profile/assets/img/avataaars.svg

390 Views