Creating a Fully Functional Login/Signup System Using Python and PySimpleGUI (Time Lapse)

Posted by


In this tutorial, we will be creating a fully functional login/signup system using Python and PySimpleGUI. PySimpleGUI is a Python package that allows you to create simple GUI applications in a clean and intuitive way.

The login/signup system we will be creating will have the following features:

  • User registration (signup) with username and password
  • User login with username and password
  • Password validation
  • Error handling for incorrect input
  • Saving user information in a file (for demonstration purposes)

To get started, make sure you have Python installed on your computer. You can download Python from https://www.python.org/. Once you have Python installed, you can install PySimpleGUI using pip by running the following command in your terminal:

pip install PySimpleGUI

Now that you have PySimpleGUI installed, let’s start creating our login/signup system. We will start by importing the necessary modules:

import PySimpleGUI as sg
import os

Next, let’s define our main function, which will create the GUI window for our login/signup system:

def main():
    layout = [
        [sg.Text('Username'), sg.InputText(key='username')],
        [sg.Text('Password'), sg.InputText(key='password', password_char='*')],
        [sg.Button('Login'), sg.Button('Signup')],
    ]

    window = sg.Window('Login/Signup System', layout)

    while True:
        event, values = window.read()

        if event == sg.WIN_CLOSED:
            break

        if event == 'Login':
            username = values.get('username')
            password = values.get('password')

            # Perform login validation here
            if login_valid(username, password):
                sg.popup('Login successful!')
            else:
                sg.popup_error('Invalid username or password')

        if event == 'Signup':
            username = values.get('username')
            password = values.get('password')

            # Perform signup validation here
            if signup_valid(username, password):
                sg.popup('Signup successful!')
            else:
                sg.popup_error('Username already exists')

    window.close()

def login_valid(username, password):
    # Perform password validation here
    return True

def signup_valid(username, password):
    # Save user information in a file (for demonstration purposes)
    filename = 'users.txt'
    if os.path.exists(filename):
        with open(filename, 'r') as file:
            users = file.readlines()

        for user in users:
            if username in user:
                return False

    with open(filename, 'a') as file:
        file.write(f'{username}:{password}n')

    return True

if __name__ == '__main__':
    main()

In the code above, we defined the layout for our GUI window, which includes input fields for username and password, as well as buttons for login and signup. We also implemented the login and signup functionality, as well as password validation and error handling.

To run the code, save it in a file (e.g., login_system.py) and run it using the following command in your terminal:

python login_system.py

You should see a GUI window pop up with the login/signup form. You can test the functionality by entering a username and password and clicking on the login or signup button.

That’s it! You have now created a fully functional login/signup system using Python and PySimpleGUI. Feel free to customize the code and add more features to suit your needs. Happy coding!

0 0 votes
Article Rating
2 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
@giovannitrivieri6722
2 months ago

Hello sir, it would be possible to have the same project for several users? I mean it would be feasible the case in which if I am already signed up then another user can signup again? looking forward

@ilikecoffee7610
2 months ago

thank you good sir