Site icon DataFlair

Python Program to Create Login Application in GUI

Master Python with 70+ Hands-on Projects and Get Job-ready - Learn Python

Starting the adventure of making graphical user interfaces (GUIs) in Python, we’re going to learn how to create a Login Application using Tkinter. A Login Form is like a basic building block in a lot of software. It makes sure only the right people can use specific parts of the program.

In this easy-to-follow guide, we’ll go through each step of creating and putting into action a Login Form using Python Tkinter. By getting good at making Login Applications, developers can make their programs more secure and user-friendly, giving users a smooth and interactive way to enter their applications.

Topic Explanation:

In Python GUI development, making a Login Application using Tkinter means thinking about how the interface looks and creating strong security checks behind the scenes. This topic will show developers how to put together a Login Form, explaining where to place things like Entry fields, Labels, and Buttons. It also covers adding logic to check if the user’s credentials are valid.

Understanding how to create a Login Application gives developers useful insights into making their applications more secure and providing users with a friendly way to log in. This exploration takes developers through the process of building a Login Form, highlighting where to put widgets like Entry fields, Labels, and Buttons on the screen. Additionally, it explains how to add a security layer by checking if the user’s credentials are valid.

Prerequisites:

Code With Comments:

# Import the Tkinter library, which is used for creating GUI applications
from tkinter import *

# Define a function named checkLogin to handle login authentication
def checkLogin():
    # Uncomment the lines below to print entered username and password
    # print("User Name:", uname.get())
    # print("Password:", upass.get())

    # Check if the entered username and password match the predefined values
    if uname.get() == 'dataflair' and upass.get() == '12345':
        # Update the label text to indicate a valid user
        lb3["text"] = "Valid User"
        lb3["fg"] = "green"
    else:
        # Update the label text to indicate an invalid user
        lb3["text"] = "Invalid User .. Login again"
        lb3["fg"] = "red"

# Create a Toplevel window (a new window)
my_root = Toplevel()

# Set the title, size, and icon of the new window
my_root.title("Login Window")
my_root.geometry('400x200')
my_root.wm_iconbitmap('2.ico')
my_root.maxsize(400, 200)
my_root.minsize(400, 200)

# Create labels for "User Name", "Password", and an empty label for displaying login status
lb1 = Label(text="User Name:", fg='blue', font=('Arial', 10, 'bold'))
lb2 = Label(text="Password:", fg='blue', font=('Arial', 10, 'bold'))
lb3 = Label(text="-", font=('Arial', 10, 'bold'))

# Place the labels in the grid layout
lb1.grid(row=0, column=0)
lb2.grid(row=1, column=0)
lb3.grid(row=2, column=0)

# Create StringVar variables to store the entered username and password
uname = StringVar()
upass = StringVar()

# Create Entry widgets for entering username and password
txtuser = Entry(textvariable=uname, font=('Arial', 10, 'bold'), fg='red')
txtpass = Entry(textvariable=upass, font=('Arial', 10, 'bold'), fg='red', show='*')

# Place the Entry widgets in the grid layout
txtuser.grid(row=0, column=1)
txtpass.grid(row=1, column=1)

# Create a Button widget for initiating the login check
btn1 = Button(text="Login", font=('Arial', 10, 'bold'), fg='#A52A2A', command=checkLogin)

# Place the Button widget in the grid layout
btn1.grid(row=3, column=0)

# Start the Tkinter event loop
my_root.mainloop()

Output:

This code creates a login window with fields for entering a username and password, a login button, and a label for displaying the login status. The user’s input is checked, and the login status is updated accordingly.

Code Explanation:

Conclusion

To wrap it up, learning how to make a Login Application with Tkinter gives developers the power to create safe and easy-to-use software. Being able to design a user-friendly Login Form, along with strong security measures, makes the whole user experience better.

By following this guide, developers can add security features to their apps, keeping user data safe. The skills gained here not only help in making secure software but also set the groundwork for more advanced GUI applications that need user authentication and authorization.

Exit mobile version