Create a Tkinter GUI to Connect to a MySQL Database

Last Updated : 29 Jul, 2026

A login interface allows users to enter credentials before accessing an application or service. In Tkinter, login forms can be created using labels, entry widgets, and buttons. This example demonstrates how to build a simple Tkinter login interface that collects database credentials and establishes a connection to a MySQL database using the mysql.connector library.

Prerequisites

  • Tkinter
  • MySQL Server
  • MySQL Connector/Python
  • MySQL Workbench (optional)

Install the required package:

pip install mysql-connector-python

Workflow

  • Enter the MySQL username and password.
  • Click Login.
  • Connect to the MySQL database.
  • Execute a sample SQL query.
  • Display the retrieved records.

Implementation

Python
import tkinter as tk
from tkinter import messagebox
import mysql.connector


def connect_to_database():

    username = username_entry.get().strip()
    password = password_entry.get().strip()

    if not username:
        messagebox.showerror("Error", "Please enter the MySQL username.")
        return

    connection = None
    cursor = None

    try:

        connection = mysql.connector.connect(
            host="localhost",
            user=username,
            password=password,
            database="College"
        )

        cursor = connection.cursor()

        query = "SELECT * FROM STUDENT"

        cursor.execute(query)

        records = cursor.fetchall()

        print("Student Records:\n")

        for row in records:
            print(row)

        messagebox.showinfo(
            "Success",
            "Connected to the database successfully.\nCheck the console for query results."
        )

    except mysql.connector.Error as err:

        messagebox.showerror(
            "Database Error",
            str(err)
        )

    finally:

        if cursor is not None:
            cursor.close()

        if connection is not None and connection.is_connected():
            connection.close()


root = tk.Tk()

root.title("MySQL Database Login")
root.geometry("360x180")
root.resizable(False, False)

# Username

tk.Label(
    root,
    text="Username"
).grid(
    row=0,
    column=0,
    padx=15,
    pady=15,
    sticky="w"
)

username_entry = tk.Entry(root, width=30)

username_entry.grid(
    row=0,
    column=1,
    padx=10,
    pady=15
)

# Password

tk.Label(
    root,
    text="Password"
).grid(
    row=1,
    column=0,
    padx=15,
    pady=10,
    sticky="w"
)

password_entry = tk.Entry(
    root,
    width=30,
    show="*"
)

password_entry.grid(
    row=1,
    column=1,
    padx=10,
    pady=10
)

# Login Button

login_button = tk.Button(
    root,
    text="Login",
    width=12,
    command=connect_to_database
)

login_button.grid(
    row=2,
    column=1,
    pady=20
)

root.mainloop()

Output:
 

python-tkinter-gui-login-dbms


 

python-tkinter-gui-login-dbms1

Explanation:

  • tkinter creates the GUI, while mysql.connector connects the application to the MySQL database.
  • connect_to_database() retrieves the username and password entered by the user.
  • mysql.connector.connect() establishes a connection using the provided database credentials.
  • connection.cursor() creates a cursor to execute SQL queries.
  • The SELECT * FROM STUDENT query retrieves all records, and fetchall() returns the query results.
  • try-except handles database connection and query errors, while messagebox displays success or error messages.
  • The finally block closes the cursor and database connection to release resources.
  • Label, Entry, and Button widgets create the login form, and grid() arranges them in rows and columns.
  • The password field uses show="*" to mask the entered password.
  • root.mainloop() starts the Tkinter event loop and keeps the application running.
Comment