From the archive

Ultimate Guide to Creating a Login Form with Username and Password in Python Using Tkinter

Build a simple Tkinter username-and-password interface in Python and learn why demonstration code is not production authentication.

Building a username-and-password interface is a useful way to learn how Tkinter labels, entry fields, buttons, callbacks, and dialog boxes work together. This tutorial creates a local demonstration interface; it does not implement secure user authentication.

The credentials in this example are hard-coded solely to demonstrate interface behavior. Do not use this pattern for a real login system, do not store passwords in source code, and do not treat masking an entry field as password protection.

Why Use Tkinter for GUI Development?

Tkinter is Python’s standard interface to the Tcl/Tk GUI toolkit and is available on macOS, Windows, and most Unix platforms. It is useful for learning desktop GUI concepts because it provides:

  1. A small standard interface: Many Python distributions make Tkinter available without a separate Python package.
  2. Cross-platform concepts: The same widget and event-loop concepts apply across supported desktop platforms.
  3. Built-in widgets: Labels, text entries, buttons, layouts, and dialog boxes cover many introductory applications.
  4. A direct event model: Button commands and the main event loop make user interactions visible in a compact example.

Tkinter is an optional CPython module, so some operating-system distributions package it separately.

Step 1: Check Python and Tkinter

Install Python from a trusted distributor, such as the official Python downloads page. Then check the interpreter from a terminal:

python --version

Run Tkinter’s built-in demonstration to confirm the module is available and see the installed Tcl/Tk version:

python -m tkinter

If your system uses python3 instead of python, use that command name consistently. If the Tkinter module is missing, follow the documentation from the distributor that supplied Python.

Step 2: Create the Application Window

Start a new Python file and create the main window:

import tkinter as tk

root = tk.Tk()
root.title("Login Form")
root.geometry("340x240")
root.resizable(False, False)

tk.Tk() creates the application window. The title and geometry methods set its label and initial size.

Step 3: Add Labels and Entry Fields

Add labeled fields for the demonstration username and password:

username_label = tk.Label(root, text="Username:")
username_label.pack(pady=(18, 4))

username_entry = tk.Entry(root)
username_entry.pack(pady=(0, 8))

password_label = tk.Label(root, text="Password:")
password_label.pack(pady=(4, 4))

password_entry = tk.Entry(root, show="*")
password_entry.pack(pady=(0, 8))

The show="*" option obscures the characters on screen. It does not encrypt, hash, transmit, or securely store the password.

Step 4: Add the Demonstration Callback

Import Tkinter’s message-box helper and define the button callback:

from tkinter import messagebox

DEMO_USERNAME = "admin"
DEMO_PASSWORD = "change-me"

def login():
    username = username_entry.get()
    password = password_entry.get()

    if username == DEMO_USERNAME and password == DEMO_PASSWORD:
        messagebox.showinfo("Demo Result", "The demo values matched.")
    else:
        messagebox.showerror("Demo Result", "The demo values did not match.")

login_button = tk.Button(root, text="Check values", command=login)
login_button.pack(pady=16)

The callback reads both entry widgets and compares them with two strings embedded in the script. That is enough to demonstrate a button command, but it is not authentication.

Step 5: Start the Event Loop

Finish the file by moving keyboard focus to the first entry field and starting Tkinter’s event loop:

username_entry.focus_set()
root.mainloop()

mainloop() keeps the window responsive until the user closes it.

Complete Demonstration

Here is the example as one runnable file:

import tkinter as tk
from tkinter import messagebox

DEMO_USERNAME = "admin"
DEMO_PASSWORD = "change-me"


def login():
    username = username_entry.get()
    password = password_entry.get()

    if username == DEMO_USERNAME and password == DEMO_PASSWORD:
        messagebox.showinfo("Demo Result", "The demo values matched.")
    else:
        messagebox.showerror("Demo Result", "The demo values did not match.")


root = tk.Tk()
root.title("Login Form")
root.geometry("340x240")
root.resizable(False, False)

username_label = tk.Label(root, text="Username:")
username_label.pack(pady=(18, 4))

username_entry = tk.Entry(root)
username_entry.pack(pady=(0, 8))

password_label = tk.Label(root, text="Password:")
password_label.pack(pady=(4, 4))

password_entry = tk.Entry(root, show="*")
password_entry.pack(pady=(0, 8))

login_button = tk.Button(root, text="Check values", command=login)
login_button.pack(pady=16)

username_entry.focus_set()
root.mainloop()

What Real Authentication Requires

A real application should not compare plain-text credentials embedded in a desktop script. Its design needs a trusted authentication boundary and a security review. Depending on the application, that can include:

  • a trusted server or approved authentication provider;
  • salted password hashing rather than reversible encryption or plain-text storage;
  • encrypted transport when credentials cross a network;
  • rate limiting or account lockout controls;
  • secure session handling and recovery flows; and
  • careful handling of logs, errors, and personally identifiable information.

Those concerns are outside this interface tutorial. The Tkinter example is intentionally limited to widgets, callbacks, and the event loop.

Interface Improvements

Once the basic example works, you can improve the interface without treating it as a real authentication system:

  • replace pack() with grid() when you need more precise alignment;
  • use tkinter.ttk widgets for the themed widget set;
  • bind the Return key to the demonstration callback;
  • add clear status text that does not rely on color alone; and
  • test keyboard navigation and focus order.