- CodeCraft by Dr. Christine Lee
- Posts
- Build Your Own Random Password Generator 🔒🎉
Build Your Own Random Password Generator 🔒🎉
A cool smart password generation tool
Welcome back, CodeCrafters!
Today, we’re diving into a super fun and practical project: building a random password generator using PySimpleGUI.
Whether you’re securing your online accounts or just love tinkering with Python, this is a great way to flex your coding muscles.
We'll walk through the process step by step, making sure everything is clear and friendly for beginners.
Ready to get started?
Let’s go!
Step 1: Setting Up Your Environment
First, make sure you have PySimpleGUI installed.
If not, open your command prompt or terminal and run:
pip install pysimplegui
This will install the library we’ll use to create our GUI (Graphical User Interface). PySimpleGUI makes it easy to build user-friendly interfaces without getting bogged down in the details.
Step 2: Designing the GUI Layout
Now, let’s start by designing the layout of our password generator.
Imagine what the app will look like: a simple window with a button to generate the password and a field to display it.
Here’s how you can set it up in Python:
import PySimpleGUI as sg
# Define the layout
layout = [
[sg.Text('Random Password Generator', size=(30, 1), font=("Helvetica", 25))],
[sg.Text('Your New Password:', size=(20, 1)), sg.InputText('', key='-PASSWORD-', size=(30, 1))],
[sg.Button('Generate', font=("Helvetica", 12)), sg.Button('Exit', font=("Helvetica", 12))]
]
# Create the window
window = sg.Window('Password Generator', layout)
Explanation:
We’re using:
sg.Text
to display textsg.InputText
for the password field, andsg.Button
for the buttons.
The
layout
variable defines how our window will be structured.
Text | InputText | Buttons
Step 3: Adding the Logic to Generate Passwords
Now comes the exciting part—adding the logic to generate random passwords.
We’ll use Python’s random
and string
libraries to create a mix of letters, numbers, and symbols.
Here’s the code:
import random
import string
def generate_password(length=12):
characters = string.ascii_letters + string.digits + string.punctuation
password = ''.join(random.choice(characters) for i in range(length))
return password
Explanation:
The
generate_password
function creates a password by randomly choosing characters from a pool of letters, numbers, and symbols.We’ve set the default length to 12 characters, but you can easily adjust this.
Step 4: Linking the Logic with the GUI
Now, we need to link our password generator with the GUI so that when you click the "Generate" button, it creates a new password.
Here’s how to do it:
while True:
event, values = window.read()
if event sg.WINDOW_CLOSED or event 'Exit':
break
if event == 'Generate':
new_password = generate_password()
window['-PASSWORD-'].update(new_password)
window.close()
Explanation:
This is where the magic happens!
We use a loop to keep the window open and check for user interactions.
When the "Generate" button is clicked, we call
generate_password()
and update the text field with the new password.
Step 5: Running Your App
That’s it!
Run your Python script, and you’ll see your shiny new password generator in action.
Click "Generate," and watch as it instantly creates a secure, random password.
# Full code example
import PySimpleGUI as sg
import random
import string
def generate_password(length=12):
characters = string.ascii_letters + string.digits + string.punctuation
password = ''.join(random.choice(characters) for i in range(length))
return password
layout = [
[sg.Text('Random Password Generator', size=(30, 1), font=("Helvetica", 25))],
[sg.Text('Your New Password:', size=(20, 1)), sg.InputText('', key='-PASSWORD-', size=(30, 1))],
[sg.Button('Generate', font=("Helvetica", 12)), sg.Button('Exit', font=("Helvetica", 12))]
]
window = sg.Window('Password Generator', layout)
while True:
event, values = window.read()
if event sg.WINDOW_CLOSED or event 'Exit':
break
if event == 'Generate':
new_password = generate_password()
window['-PASSWORD-'].update(new_password)
window.close()
Random Password Generator
Wrapping Up
You’ve just built a random password generator with PySimpleGUI! 🎉
Not only is this project useful, but it’s also a great way to get comfortable with Python and GUI development.
As you can see, PySimpleGUI makes it super easy to create user-friendly apps.
Coding with a Smile 🤣 😂
The Loop-de-Loop:
Writing nested loops can feel like trying to untangle holiday lights. It looks simple at first, but soon you’re deep in a tangle wondering how it got so complicated.
Recommended Resources 📚
Big Desk Energy is by Tyler Denk who is currently the Co-Founder/CEO at Beehiiv. Before this, he was the second employee at Morning Brew, where he spent his days up until the acquisition by Business Insider.
I gladly recommend his weekly newsletter where he shared his experiences, processes, and philosophies as a startup founder with a lot of strong opinions and absolutely no filter.
Let’s Inspire Future AI Coders Together! ☕
I’m excited to continue sharing my passion for Python programming and AI with you all. If you’ve enjoyed the content and found it helpful, do consider supporting my work with a small gift. Just click the link below to make a difference – it’s quick, easy, and every bit helps and motivates me to keep creating awesome contents for you.
Thank you for being amazing!
What’s Next? 📅
Get ready for some cryptographic fun in our next post! 🎉
We’ll be diving into the world of classic ciphers by building a Caesar cipher app using PySimpleGUI.
Whether you’re new to encryption or just love a good coding challenge, this project will help you understand the basics of ciphers while flexing your Python skills.
Plus, with a user-friendly GUI, encrypting and decrypting messages will be as easy as pie.
Stay tuned! 🕵️♂️🔐
Ready for More Python Fun? 📬
Subscribe to our newsletter now and get a free Python cheat sheet! 📑 Dive deeper into Python programming with more exciting projects and tutorials designed just for beginners.
Keep learning, keep coding 👩💻👨💻, and keep discovering new possibilities! 💻✨
Enjoy your journey into artificial intelligence, machine learning, data analytics, data science and more with Python!
Stay tuned for our next exciting project in the following edition!
Happy coding!🚀📊✨
🎉 We want to hear from you! 🎉 How do you feel about our latest newsletter? Your feedback will help us make it even more awesome! |