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!

Login or Subscribe to participate in polls.