- CodeCraft by Dr. Christine Lee
- Posts
- Build Your Own Login App with PySimpleGUI: Checking Username and Password! πβ¨
Build Your Own Login App with PySimpleGUI: Checking Username and Password! πβ¨
Young tech savvy AI-girl hacker
Hello, CodeCrafters! π
Ready to build something cool and useful?
Today, weβre diving into creating a simple login app using PySimpleGUI!
We'll learn how to check a username and password. This project is fun, easy, and a fantastic way to level up your coding skills!
Let's get started! π
Step 1: Install PySimpleGUI
First things first, if you havenβt installed PySimpleGUI yet, open your terminal or command prompt and type:
pip install pysimplegui
Now that you have PySimpleGUI installed, weβre all set to start coding!
Step 2: Import the Library
We'll start by importing PySimpleGUI:
import PySimpleGUI as sg
Step 3: Design the Layout
Let's design our login window. We need fields for the username and password, and a button to submit the login details.
layout = [
[sg.Text('Username'), sg.InputText(key='-USERNAME-')],
[sg.Text('Password'), sg.InputText(key='-PASSWORD-', password_char='*')],
[sg.Button('Login'), sg.Button('Cancel')]
]
Step 4: Create the Window
Next, we create a window with the specified layout:
window = sg.Window('Login App', layout)
Step 5: Event Loop (The Fun Part!)
Here's where the magic happens! Weβll create an event loop that checks for button clicks and retrieves the input values.
while True:
event, values = window.read()
if event sg.WIN_CLOSED or event 'Cancel':
break
username = values['-USERNAME-']
password = values['-PASSWORD-']
if username 'admin' and password 'secret':
sg.popup('Login Successful!')
else:
sg.popup('Login Failed. Try Again.')
window.close()
Step 6: Putting It All Together
Hereβs the complete code:
import PySimpleGUI as sg
layout = [
[sg.Text('Username'), sg.InputText(key='-USERNAME-')],
[sg.Text('Password'), sg.InputText(key='-PASSWORD-', password_char='*')],
[sg.Button('Login'), sg.Button('Cancel')]
]
window = sg.Window('Login App', layout)
while True:
event, values = window.read()
if event sg.WIN_CLOSED or event 'Cancel':
break
username = values['-USERNAME-']
password = values['-PASSWORD-']
if username 'admin' and password 'secret':
sg.popup('Login Successful!
π')
else:
sg.popup('Login Failed. Try Again.
π')
window.close()
Step-by-Step Breakdown:
1. Designing the Layout:
We create text labels and input fields for the username and password. The
password_char='*'
ensures that the password is hidden when typed.
2. Creating the Window:
We initialize the window with the layout we designed.
3. Event Loop:
The loop reads events (like button clicks) and values (input text).
If the window is closed or 'Cancel' is clicked, the loop breaks, and the window closes.
It checks if the username is 'admin' and the password is 'secret'. If they match, a success message pops up. If not, an error message is displayed.
Output
Login Successful
Login Failed
Why This Is Fun and Useful:
Real-World Application: Creating a login system is a fundamental skill in app development.
Interactive Learning: You get to see immediate results of your code, making learning more engaging.
Customizable: You can easily extend this app to check against a database or file, add more features, and make it your own!
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? π
Coming Up Next: Supercharge Your Apps with PySimpleGUI Themes! πβ¨
Get ready to take your PySimpleGUI skills to the next level! In our upcoming post, we'll dive into the world of PySimpleGUI themes and show you how to transform your apps with just a few lines of code. Imagine turning your basic GUI into a vibrant, visually stunning masterpiece that not only works smoothly but also wows your users. We'll guide you through selecting and applying themes to create a truly captivating user experience. This is going to be a game-changer for your projects, so stay tuned and prepare to unleash your creativity! ππ
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! |