Install the necessary module:
The pyperclip module is used in this tutorial to copy the randomly generated Password to use it somewhere. Run the following command to install pyperclip.
Example-1: Implement a simple password generator
The simple way to create a password generator has shown in the following script. A large text of mixed characters has been declared to generate the Password by selecting the particular length characters. The length of the Password will be taken from the user. sample() function of the random module has been used to select the character from the text and join together randomly. The randomly generated password will be printed later.
import random
# Set the character list for generating the password
characters = "01234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ&*(){}[]|/\?!@#$%^abcdefghijklmnopqrstuvwxyz"
# Take the length of the password from the user
password_length = int(input('Enter the length of the password: '))
# Generate the password
password = "".join(random.sample(characters, password_length))
# Print the generated password
print("Gernerated password: %s" %password)
Output:
12 has given as the value of the Password length, and a password of 12 characters has been generated after executing the script.
Example-2: Implement Password Generator with GUI
GUI-based password generator application is helpful for the user to generate the random Password. Tkinter module has been imported into the script to create the user-friendly password generator application. Random and string modules have been imported to generate the random Password by mixing the uppercase, lowercase, digit, and special characters. The pyperclip module is used to copy the generated Password for creating different types of user accounts. Password_Generator() function has defined to generate the Password and store it in the password variable. CopyPassword() function has defined to copy the generated Password after generating the Password. A Tkinter object has been declared to define the application window where the required widgets for designing the password generator application will be added. The first label widget has defined setting the label for the spin box widget to enter the Password length. The second label widget has defined the text box widget label where the generated Password will be displayed. Two-button widgets have been defined to generate the Password and copy the Password. When the ‘Generate Password’ button is pressed, the Password_Generator() function will be called, and the randomly generated password will be displayed in the text box. After generating the Password, if the ‘Copy’ button will be pressed, then the CopyPassword() function will be called to copy the Password, and the text ‘Password copied‘ will be shown above the buttons.
import tkinter
# Import random and string modules
import random, string
# Import pyperclip module
import pyperclip
# Define function to generate random password
def Password_Generator():
password = ''
for n in range(lenPassword.get()):
password = password+random.choice(string.ascii_uppercase + string.ascii_lowercase + string.digits + string.punctuation)
strPassword.set(password)
# Define function to copy the generated password
def CopyPassword():
if strPassword.get() != '':
pyperclip.copy(strPassword.get())
lblmsg2 = tkinter.Label(win, text='Password copied', font='Ubuntu 12 bold',width=15)
lblmsg2.place(x=105, y=120)
else:
lblmsg2 = tkinter.Label(win, text='Nothing to copy', font='Ubuntu 12 bold', width=15)
lblmsg2.place(x=105, y=120)
# Define object to display the main window of the application
win = tkinter.Tk()
# Set the title of the main window
win.title("Random Password Generator")
# Set the height and width of the main window
win.geometry("350x220")
# Set the position of the window
win.eval('tk::PlaceWindow . center')
# Set label for the password length
lblPassword = tkinter.Label(win, text='Set Password Length', font='Ubuntu 15 bold')
lblPassword.pack()
# Set the data type of the password length
lenPassword = tkinter.IntVar()
# Set the limit for the length value
length = tkinter.Spinbox(win, from_=6, to_=16, textvariable=lenPassword, width=2)
length.pack(pady=10)
# Set the label for the password field
lblmsg1 = tkinter.Label(win, text='Generated Password', font='Ubuntu 12')
lblmsg1.pack()
# Set the data type of the password field
strPassword = tkinter.StringVar()
# Define the variable for the password field
textData=tkinter.Entry(win, textvariable=strPassword, width=15)
textData.pack()
# Define button to call the function to generate the random password
btnPassword=tkinter.Button(win, text="Generate Password", command=Password_Generator)
btnPassword.pack(padx=50,pady=5,side=tkinter.LEFT)
# Define button to call the function to copy the password
btnCopy=tkinter.Button(win, text='Copy', command = CopyPassword)
btnCopy.pack(side=tkinter.LEFT)
# Call the mainloop of Tkinter to open the main window
win.mainloop()
Output:
The following window will appear after executing the script. 6 has been set as the default length value of the Password. The user can increase or decrease the value before generating the Password.
The Password of 8 characters has been generated after setting the length value to 8 and pressing the Generate Password button.
Password copied message has appeared after pressing the Copy button. Now, this Password can be used in other applications to create a user account.
Conclusion:
Two different ways of creating random Passwords have been explained in this tutorial. The first example can be used if you want to create a text-based password generator, and the second example can be used if you want to create GUI based password generator.