Create a Stylish Tkinter Login System with SQLite | Python GUI Project
Demo :
Click Video πππ
Code :
import sqlite3
import tkinter as tk
from tkinter import messagebox
# Database Setup
conn = sqlite3.connect("database.db")
cursor = conn.cursor()
cursor.execute('''CREATE TABLE IF NOT EXISTS users (id INTEGER PRIMARY KEY, username TEXT UNIQUE, password TEXT)''')
conn.commit()
# Tkinter Window Setup
root = tk.Tk()
root.title("Modern Tkinter Login - FuzzuTech")
root.geometry("420x400")
root.config(bg="#1e272e") # Dark theme background
# Header Label
tk.Label(root, text="π FuzzuTech Login", font=("Arial", 16, "bold"), bg="#1e272e", fg="#f1c40f").pack(pady=15)
# Username Label + Entry
tk.Label(root, text="Username:", bg="#1e272e", fg="white", font=("Arial", 12)).pack(anchor="w", padx=30)
username_entry = tk.Entry(root, font=("Arial", 12), bg="#dfe6e9", fg="black", width=30, relief="flat", highlightthickness=1)
username_entry.pack(pady=5, padx=30)
# Password Label + Frame (for Eye Button)
tk.Label(root, text="Password:", bg="#1e272e", fg="white", font=("Arial", 12)).pack(anchor="w", padx=30)
password_frame = tk.Frame(root, bg="#1e272e")
password_frame.pack(pady=5, padx=30, fill="x")
password_entry = tk.Entry(password_frame, font=("Arial", 12), show="*", bg="#dfe6e9", fg="black", width=25, relief="flat", highlightthickness=1)
password_entry.pack(side="left", fill="x", expand=True)
# Eye Button to Show/Hide Password
def toggle_password():
if password_entry.cget('show') == "*":
password_entry.config(show="")
else:
password_entry.config(show="*")
toggle_btn = tk.Button(password_frame, text="π", command=toggle_password, font=("Arial", 10), bg="#3498db", fg="white", relief="flat", width=3)
toggle_btn.pack(side="right")
# Login Function
def login():
username = username_entry.get().strip()
password = password_entry.get().strip()
if not username or not password:
messagebox.showerror("Error", "⚠ Username and Password cannot be empty!")
return
conn = sqlite3.connect("database.db")
cursor = conn.cursor()
cursor.execute("SELECT * FROM users WHERE username=? AND password=?", (username, password))
user = cursor.fetchone()
conn.close()
if user:
messagebox.showinfo("Success", "✅ Login Successful!")
root.destroy()
open_dashboard()
else:
messagebox.showerror("Error", "❌ Invalid Username or Password!")
# Register Function
def register():
username = username_entry.get().strip()
password = password_entry.get().strip()
if not username or not password:
messagebox.showerror("Error", "⚠ Username and Password cannot be empty!")
return
conn = sqlite3.connect("database.db")
cursor = conn.cursor()
try:
cursor.execute("INSERT INTO users (username, password) VALUES (?, ?)", (username, password))
conn.commit()
messagebox.showinfo("Success", "π Registration Successful!")
except sqlite3.IntegrityError:
messagebox.showerror("Error", "⚠ Username already exists!")
conn.close()
# Open Dashboard
def open_dashboard():
dashboard = tk.Tk()
dashboard.title("Dashboard")
dashboard.geometry("350x200")
dashboard.config(bg="#34495e")
tk.Label(dashboard, text="π― Welcome to Dashboard!", font=("Arial", 14, "bold"), bg="#34495e", fg="white").pack(pady=30)
dashboard.mainloop()
# Buttons with Gradient Style
login_btn = tk.Button(root, text="LOGIN", command=login, font=("Arial", 12, "bold"), bg="#27ae60", fg="white", relief="flat", width=15, height=1)
login_btn.pack(pady=10)
register_btn = tk.Button(root, text="REGISTER", command=register, font=("Arial", 12, "bold"), bg="#2980b9", fg="white", relief="flat", width=15, height=1)
register_btn.pack(pady=5)
# Run App
root.mainloop()
Comments
Post a Comment