DEV Community

Pandeyashish17
Pandeyashish17

Posted on

Creating Secure Passwords with Python and Tkinter: The "Password Generator" Code

Hey guys, are you tired of coming up with new and creative passwords for all of your online accounts? Well, have no fear! The "Password Generator" is here to save the day.

code:

import tkinter as tk
import random
import clipboard

def generate_password():
    length = length_var.get()
    include_capital_letters = capital_letters_var.get()
    include_special_symbols = special_symbols_var.get()
    include_numbers = numbers_var.get()
    include_small_letters = small_letters_var.get()

    password = ""
    for i in range(length):
        if include_capital_letters and random.random() > 0.5:
            password += chr(random.randint(65, 90))
        elif include_small_letters and random.random() > 0.5:
            password += chr(random.randint(97, 122))
        elif include_special_symbols and random.random() > 0.5:
            password += chr(random.randint(33, 47))
        elif include_numbers and random.random() > 0.5:
            password += chr(random.randint(48, 57))
        else:
            password += chr(random.randint(97, 122))

    password_entry.delete(0, tk.END)
    password_entry.insert(0, password)

def copy_password():
    clipboard.copy(password_entry.get())

root = tk.Tk()
root.title("Password Generator")
root.configure(bg='aqua')

length_var = tk.IntVar()
length_var.set(8) 

capital_letters_var = tk.BooleanVar()
capital_letters_var.set(True)

small_letters_var = tk.BooleanVar()
small_letters_var.set(True)

special_symbols_var = tk.BooleanVar()
special_symbols_var.set(True)

numbers_var = tk.BooleanVar()
numbers_var.set(True)

length_label = tk.Label(root, text="Length:", font=("Helvetica", 20), bg='aqua')
length_label.pack()

length_entry = tk.Entry(root, textvariable=length_var, font=("Helvetica", 20))
length_entry.pack()

capital_letters_checkbox = tk.Checkbutton(root, text="Include capital letters", variable=capital_letters_var, font=("Helvetica", 20), bg='aqua')
capital_letters_checkbox.pack()

small_letters_checkbox = tk.Checkbutton(root, text="Include small letters", variable=small_letters_var, font=("Helvetica", 20), bg='aqua')
small_letters_checkbox.pack()

special_symbols_checkbox = tk.Checkbutton(root, text="Include special symbols", variable=special_symbols_var, font=("Helvetica", 20), bg='aqua')
special_symbols_checkbox.pack()

numbers_checkbox = tk.Checkbutton(root, text="Include numbers", variable=numbers_var, font=("Helvetica", 20), bg='aqua')
numbers_checkbox.pack()

generate_button = tk.Button(root, text="Generate Password", command=generate_password, font=("Helvetica", 20), bg='aqua')
generate_button.pack()

password_entry = tk.Entry(root, font=("Helvetica", 20), bg='aqua')
password_entry.pack()

copy_button = tk.Button(root, text="Copy to Clipboard", command=copy_password, font=("Helvetica", 20), bg='aqua')
copy_button.pack()

root.mainloop()

Enter fullscreen mode Exit fullscreen mode

output:

password generator GUIin python

This handy dandy piece of code, written in the powerful programming language Python, uses the Tkinter library to create a user-friendly interface that allows you to customize the length and character set of your new password.

But wait, there's more! Not only does this bad boy generate a brand new, never-before-used password for you, it also has the capability to copy it straight to your clipboard. That's right, no more manually selecting, copying, and pasting. This code does it all for you.

But don't just take our word for it. Try it out for yourself and see the magic happen.

So don't waste another second using "password123" or "qwerty" as your go-to password. Let the "Password Generator" handle it and sit back and relax, knowing that your online accounts are safe and secure.

In short, this code is a simple password generator with a graphical user interface which allows the user to choose the length of password and also the types of characters (capital letters, small letters, special symbols, numbers) to include in the password and copy the password to clipboard.

This code uses the Tkinter library to create a graphical user interface (GUI) for the user to interact with. The user can select the desired length of the password, and whether or not to include capital letters, small letters, special symbols, and numbers in the password.

The generate_password() function is responsible for creating the password based on the user's selections. It uses a for loop to iterate through the desired length of the password, and within the loop, it uses a series of if/elif statements and the random module to determine which type of character (capital letter, small letter, special symbol, or number) to add to the password.

The copy_password() function is used to copy the generated password to the clipboard, so the user can easily paste it into the desired account.

Overall, this code is a simple, yet effective way to generate a secure password with the added convenience of being able to copy it to the clipboard. So, next time you need a new password, give this code a try!

Top comments (0)