Login Form GUI in Python

PHOTO EMBED

Sat Jul 15 2023 15:19:13 GMT+0000 (Coordinated Universal Time)

Saved by @pythonHub #python #python-hub #day12

from customtkinter import *
import pandas as pd

class LoginFrame(CTkFrame):
    """
    A frame for login functionality.
    """
    def __init__(self, master, **kwargs):
        super().__init__(master, **kwargs)
        """
        Initialize the LoginFrame.

        Args:
            master: The parent widget.
            kwargs: Additional keyword arguments for the frame.
        """
        # Create and grid the login label
        self.log = CTkLabel(self, text="LOG IN", text_color="#ffe9a6", font=("Verdana",20))
        self.log.grid(row=0, column=0,padx=10, pady=10)

        # Create and grid the username entry field
        self.user = CTkEntry(self, placeholder_text="Username", text_color="white", fg_color="#030126", border_color="#030126")
        self.user.grid(row=1, column=0,padx=10, pady=10)

        # Create and grid the password entry field
        self.password = CTkEntry(self, placeholder_text="Password", show="*", text_color="white", fg_color="#030126", border_color="#030126")
        self.password.grid(row=2, column=0,padx=10, pady=10)

        # Create and grid the submit button
        self.submit = CTkButton(self, text="Submit", fg_color="#fccc3d", text_color="#2e3140", hover=False, command=self.check, font=("arial",14))
        self.submit.grid(row=3, column=0,padx=10, pady=10)

    def check(self):
        """
        Check if the entered username and password are valid.
        """

        user = self.user.get()
        if self.find_user(user):
            passw = self.password.get()
            if self.match_password(user, passw):
                # Clear the app window and show success message
                for widget in app.winfo_children():
                    widget.destroy()
                label = CTkLabel(app, text="Successfully Logged In!!")
                label.grid(row=1, column=1, padx=10, pady=10)
            else:
                # Show invalid password message
                label = CTkLabel(app, text="Invalid Password")
                label.grid(row=1, column=1, padx=10, pady=10)
        else:
            # Show invalid username message
            label = CTkLabel(app, text="Invalid Username")
            label.grid(row=1, column=1, padx=10, pady=10)

    def find_user(self, username:str) -> bool:
        """
        Find if a user with the given username exists.

        Args:
            username: The username to search for.

        Returns:
            bool: True if the username exists, False otherwise.
        """
        df = pd.read_csv("users.csv")
        for i in range(len(df)):
            cuser = list(df.loc[i])[0]
            if username ==cuser:
                return True
        else:
            return False
        
    def match_password(self, username:str, password:str) -> bool:
        """
        Check if the given password matches the username.

        Args:
            username: The username to check.
            password: The password to match.

        Returns:
            bool: True if the password matches, False otherwise.
        """
        df = pd.read_csv("users.csv")
        for i in range(len(df)):
            cuser = list(df.loc[i])[0]
            passw = list(df.loc[i])[1]
            if username ==cuser:
                if password == passw:
                    return True
                return False


app = CTk()

frame = LoginFrame(master=app)
frame.grid(row=0, column=1,padx=10, pady=10)

app.mainloop()
content_copyCOPY