Answer: Build the form with tkinter Labels, Entry boxes and a Submit button, connect to MySQL with mysql.connector, and in the button's function run a parameterised INSERT query followed by commit().
Step 1: Install the connector and create the table
pip install mysql-connector-pythonCREATE DATABASE college;
USE college;
CREATE TABLE users (
id INT AUTO_INCREMENT PRIMARY KEY,
first_name VARCHAR(50) NOT NULL,
last_name VARCHAR(50),
email VARCHAR(100) NOT NULL,
password CHAR(64) NOT NULL
);Step 2: How the program works
- mysql.connector.connect(host, user, password, database) opens the connection; cursor() gives an object to run SQL.
- tk.Tk() creates the window; Label and Entry widgets are placed with grid(row, column). show="*" hides the password.
- submit() reads the boxes with get(), checks required fields, and runs INSERT ... VALUES (%s, %s, %s, %s) with a tuple of values. Using %s placeholders (not string joining) prevents SQL injection.
- mydb.commit() saves the row; a message box confirms and the boxes are cleared.
- The password is stored as a SHA-256 hash, not as plain text. (For a real application use a slow password hash such as bcrypt.)
- window.mainloop() keeps the window open.
Complete code
import hashlib
import tkinter as tk
from tkinter import messagebox
import mysql.connector
# 1. Connect to MySQL (change user/password/database to yours)
mydb = mysql.connector.connect(
host="localhost",
user="root",
password="your_password",
database="college"
)
mycursor = mydb.cursor()
# 2. Function called by the Submit button
def submit():
fname = first_name.get().strip()
lname = last_name.get().strip()
mail = email.get().strip()
pwd = password.get()
if not fname or not mail or not pwd:
messagebox.showerror("Error", "First name, email and password are required")
return
# store a hash, never the plain password
pwd_hash = hashlib.sha256(pwd.encode()).hexdigest()
sql = ("INSERT INTO users (first_name, last_name, email, password) "
"VALUES (%s, %s, %s, %s)")
mycursor.execute(sql, (fname, lname, mail, pwd_hash))
mydb.commit()
messagebox.showinfo("Success", "Registration successful")
print("Data Submitted!")
for box in (first_name, last_name, email, password):
box.delete(0, tk.END)
# 3. Window, labels and entry boxes
window = tk.Tk()
window.title("Registration Form")
tk.Label(window, text="First Name").grid(row=0, column=0, padx=10, pady=5, sticky="w")
tk.Label(window, text="Last Name").grid(row=1, column=0, padx=10, pady=5, sticky="w")
tk.Label(window, text="Email").grid(row=2, column=0, padx=10, pady=5, sticky="w")
tk.Label(window, text="Password").grid(row=3, column=0, padx=10, pady=5, sticky="w")
first_name = tk.Entry(window)
last_name = tk.Entry(window)
email = tk.Entry(window)
password = tk.Entry(window, show="*")
first_name.grid(row=0, column=1, padx=10)
last_name.grid(row=1, column=1, padx=10)
email.grid(row=2, column=1, padx=10)
password.grid(row=3, column=1, padx=10)
# 4. Submit button
submit_button = tk.Button(window, text="Submit", command=submit)
submit_button.grid(row=4, column=1, pady=10)
# 5. Start the GUI
window.mainloop()Test: the script was run with Python 3 using an offline stand-in for MySQL (an in-memory SQLite table with the same columns); entering Sneha / Patil / sneha@example.com / secret123 and pressing Submit gave:
[messagebox] Success - Registration successful
Data Submitted!
[db] [('Sneha', 'Patil', 'sneha@example.com', 'fcf730b6d95236ecd3c9fc2d92d7b6b2bb061514961aec041d6c7a7192f592e4')]With a real MySQL server the same row appears in SELECT * FROM users;.