Create Login page design in Tkinter

This Python script demonstrates how you can develop a user registration and login system using the Tkinter library. To begin with, it creates a well-designed graphical user interface (GUI) that not only facilitates account creation but also provides a secure login process and effective credential management. Additionally, the application features a simple yet efficient interface where users can effortlessly sign up for new accounts, log in to their existing accounts, and manage their credentials. Furthermore, this script highlights several techniques for building a robust and user-friendly registration and login system, allowing you to design an intuitive GUI that supports essential user management functions. Consequently, you will learn best practices for developing secure and accessible applications for users.

                  User Registration & Login using Tkinter

This Python script demonstrates how to use the Tkinter library to build a graphical user interface (GUI) for user registration and login. First, you will learn to register new users. Next, you will enable user logins for existing accounts, and finally, you will display the saved user credentials

Library Imports

The script begins by importing the necessary modules: tkinter for GUI elements, message box for displaying messages, and os for file operations.

Save User Details Function

A function named save_user_details is defined. This function opens a file named “user_details.txt” in write mode. It iterates through the user_details dictionary, writing each username and password pair to the file in the format “username”.The function writes each pair on a new line.

 Load User Details Function

A function named load_user_details is defined. This function checks if the file “user_details.txt” exists. If it does, the function opens the file in read mode. It reads each line of the file, extracts the username and password, and adds them to the user_details dictionary.

 User Details Dictionary

An empty dictionary named user_details is defined to store usernames and passwords. You call the load_user_details function to populate this dictionary with any existing user details from the file.

 Login Function

A function named login is defined. This function retrieves the username and password entered by the user in the respective entry fields. It checks if the username exists in the user_details dictionary. If the username exists, it verifies if the entered password matches the stored password. If the password matches, the function displays an information message box to indicate a successful login. Otherwise, the function displays an error message box. If the username does not exist, the function displays an error message box

  Handle Signup Function

A function named handling_signup is defined. This function retrieves the new username, new password, and re-entered password from the respective entry fields. It checks if the new username already exists in the user_details dictionary. If it does, the function displays an error message box.

If the passwords do not match, the function displays an error message box. In case the username is unique and the passwords match, the function adds the new username and password to the user_details dictionary. The function displays an information message box to indicate the creation of the new account. The function saves the user details to the file using the save_user_details function and closes the signup window.

Open Signup Window Function

A function named opening_signup_window is defined. This function creates a new top-level window for user registration. You create and pack labels and entry fields for the new username, new password, and re-entered password into the window. You create and pack a button into the window. Subsequently, when you click this button, it calls the handling_signup function. This action facilitates the signup process.

Main Window Setup

The main window is created using tk.Tk(), and its title is set to “Login Page”. You create and pack labels and entry fields for the username and password into the main window. First, you create and pack a login button into the main window. When you click this button, it triggers the login function. Besides this, you create and pack a signup button into the main window. Eventually, clicking the signup button will call the opening_signup_window function.

Main Loop

The script ends with a call to root.mainloop(), initiating the Tkinter event loop. As a result, the event loop makes the GUI interactive and responsive to user actions. In this way, root.mainloop() continuously processes events like button clicks and text entries, therefore keeping the GUI active and accessible throughout the application’s duration.

import tkinter as tk
from tkinter import messagebox
import os

def save_user_details():
    with open("user_details.txt", "w") as f:
        for username, password in user_details.items():
            f.write(f"{username}:{password}\n")

def load_user_details():
    if os.path.exists("user_details.txt"):
        with open("user_details.txt", "r") as f:
            for i in f:
                username, password = i.strip().split(":")
                user_details[username] = password

user_details = {}
load_user_details()

def login():
    username = entry_username.get()
    password = entry_password.get()
    if username in user_details:
        if user_details[username] == password:
            messagebox.showinfo("Login Successful", f"Welcome, {username}")
        else:
            messagebox.showerror("Login Unsuccessful", "Wrong Password")
    else:
        messagebox.showerror("Login Unsuccessful", "Username is not registered")

def handling_signup(entry_new_username, entry_new_password, entry_Re_enter_password, signup_window):
    new_username = entry_new_username.get()
    new_password = entry_new_password.get()
    re_enter_password = entry_Re_enter_password.get()
    
    if new_username in user_details:
        messagebox.showerror("Username already exists!", "Please choose a different username.")
    elif new_password != re_enter_password:
        messagebox.showerror("Passwords do not match", "Please re-enter your passwords.")
    else:
        user_details[new_username] = new_password
        messagebox.showinfo("Account created", f"Account created for {new_username}.")
        save_user_details()
        signup_window.destroy()

def opening_signup_window():
    global entry_new_username, entry_new_password, entry_Re_enter_password
    
    signup_window = tk.Toplevel(root)
    signup_window.title("Sign Up Page")

    label_new_username = tk.Label(signup_window, text="New Username:")
    label_new_username.pack(pady=8)
    entry_new_username = tk.Entry(signup_window)
    entry_new_username.pack(pady=8)

    label_new_password = tk.Label(signup_window, text="New Password:")
    label_new_password.pack(pady=8)
    entry_new_password = tk.Entry(signup_window, show="#")
    entry_new_password.pack(pady=8)

    label_Reenter_new_password = tk.Label(signup_window, text="Re-enter Password:")
    label_Reenter_new_password.pack(pady=8)
    entry_Re_enter_password = tk.Entry(signup_window, show="#")
    entry_Re_enter_password.pack(pady=8)

    signup_button = tk.Button(signup_window, text="Sign Up", width=15, command=lambda: handling_signup(entry_new_username, entry_new_password, entry_Re_enter_password, signup_window))
    signup_button.pack(pady=15)

root = tk.Tk()
root.title("Login Page")

label_username = tk.Label(root, text="Username:")
label_username.pack(pady=10)
entry_username = tk.Entry(root)
entry_username.pack(pady=5)

label_password = tk.Label(root, text="Password:")
label_password.pack(pady=10)
entry_password = tk.Entry(root, show="#")
entry_password.pack(pady=5)

login_button = tk.Button(root, text="Login", width=10, command=login)
login_button.pack(pady=10)

signup_button = tk.Button(root, text="Sign Up", width=10, command=opening_signup_window)
signup_button.pack()

root.mainloop()
INPUT:
username: sai
password: sai
OUTPUT:
Welcome, sai
video while running the program:

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top