Ex.
No:
Vehicle Registration System
Date:
Aim:
To develop a Vehicle Registration System using Python and SQLite to demonstrate
fundamental database management operations such as insertion, retrieval, deletion, and
display of data through a graphical user interface.
PROCEDURE:
1. Database Design:
Design a table named vehicle with appropriate fields: id, owner, number, model, phone,
address, and date.
Set id as the primary key and number as a unique field to prevent duplicate vehicle
entries.
2. Database Creation:
Use SQLite as the backend database.
Write Python code to connect to the SQLite database and create the table if it does not
exist (vehicle_backend.py).
3. GUI Development:
Use Python's Tkinter library to design the graphical user interface (app.py).
Create input fields for owner name, vehicle number, model, phone number, and address.
Add buttons for operations: Register, Search, Delete, and Refresh.
4. Function Implementation:
Implement the register_vehicle() function to insert vehicle details into the database.
Implement the search_vehicle() function to retrieve details of a vehicle by its number.
Implement the delete_vehicle() function to remove a vehicle record from the database.
Implement the view_all_vehicles() function to display all vehicle entries in a tabular
format.
5. Validation:
Validate user inputs to ensure all fields are filled.
Restrict phone number input to digits only and a maximum length of 10.
6. Testing and Execution:
Run the program using the Python interpreter.
Test each functionality (Register, Search, Delete, Refresh) with sample inputs.
Ensure that the GUI responds correctly and the database reflects all changes.
Program:
App.Py
import tkinter as tk
from tkinter import ttk, messagebox
from PIL import Image, ImageTk
from vehicle_backend import register_vehicle, search_vehicle, delete_vehicle,
view_all_vehicles
# Main window setup
root = tk.Tk()
root.title("Vehicle Registration System")
root.state('zoomed') # Fullscreen
# Screen size
screen_width = root.winfo_screenwidth()
screen_height = root.winfo_screenheight()
# Background image
bg_image = Image.open(r"C:\Users\Harish.p\Downloads\online-games-
concept\vehicle1.jpg")
bg_image = bg_image.resize((screen_width, screen_height), Image.LANCZOS)
bg_photo = ImageTk.PhotoImage(bg_image)
# Set background
bg_label = tk.Label(root, image=bg_photo)
bg_label.place(x=0, y=0, relwidth=1, relheight=1)
bg_label.lower()
# Variables
owner_name = tk.StringVar()
vehicle_number = tk.StringVar()
model = tk.StringVar()
phone = tk.StringVar()
address = tk.StringVar()
# Functions
def register():
name = owner_name.get().strip()
number = vehicle_number.get().strip()
car_model = model.get().strip()
phone_no = phone.get().strip()
addr = address.get().strip()
if name and number and car_model and phone_no and addr:
result = register_vehicle(name, number, car_model, phone_no, addr)
messagebox.showinfo("Success", result)
clear_entries()
refresh_data()
else:
messagebox.showwarning("Input Error", "Please fill all fields.")
def search():
number = vehicle_number.get().strip()
if number:
result = search_vehicle(number)
if result:
messagebox.showinfo("Search Result",
f"Owner: {result[1]}\nVehicle No: {result[2]}\nModel: {result[3]}"
f"\nPhone: {result[4]}\nAddress: {result[5]}\nDate: {result[6]}")
else:
messagebox.showinfo("Not Found", "Vehicle not found.")
else:
messagebox.showwarning("Input Error", "Enter vehicle number to search.")
def delete():
number = vehicle_number.get().strip()
if number:
result = delete_vehicle(number)
messagebox.showinfo("Delete", result)
refresh_data()
else:
messagebox.showwarning("Input Error", "Enter vehicle number to delete.")
def refresh_data():
for row in tree.get_children():
tree.delete(row)
for record in view_all_vehicles():
tree.insert("", "end", values=record)
def clear_entries():
owner_name.set("")
vehicle_number.set("")
model.set("")
phone.set("")
address.set("")
owner_entry.focus()
def focus_next(event, next_widget):
next_widget.focus()
return "break"
# Button style
button_style = {
"bg": "#4CAF50",
"fg": "white",
"activebackground": "#45a049",
"activeforeground": "white",
"width": 12,
"bd": 2,
"relief": "raised",
"font": ("times new roman", 12, "bold")
}
def on_register_click(btn):
btn.config(bg="orange") # temporary color
root.after(1000, lambda: btn.config(bg="#4CAF50")) # restore after 1 second
register() # call your actual register logic
register_button = tk.Button(root, text="Register", **button_style)
register_button.config(command=lambda: on_register_click(register_button))
register_button.place(x=250, y=300)
# Labels and Entry Fields
tk.Label(root, text="Name", font=("times new roman", 12), fg="#ADEFD1",
bg="#00203F").place(x=100, y=50)
owner_entry = tk.Entry(root, textvariable=owner_name, width=30, font=("times new roman",
12))
owner_entry.place(x=250, y=50)
owner_entry.bind("<Return>", lambda e: focus_next(e, vehicle_entry))
tk.Label(root, text="Vehicle Number", font=("times new roman", 12), fg="#ADEFD1",
bg="#00203F").place(x=100, y=100)
vehicle_entry = tk.Entry(root, textvariable=vehicle_number, width=30, font=("times new
roman", 12))
vehicle_entry.place(x=250, y=100)
vehicle_entry.bind("<Return>", lambda e: focus_next(e, model_entry))
tk.Label(root, text="Model", font=("times new roman", 12), fg="#ADEFD1",
bg="#00203F").place(x=100, y=150)
model_entry = tk.Entry(root, textvariable=model, width=30, font=("times new roman", 12))
model_entry.place(x=250, y=150)
model_entry.bind("<Return>", lambda e: focus_next(e, phone_entry))
def validate_phone(P):
return P.isdigit() and len(P) <= 10
vcmd = root.register(validate_phone)
tk.Label(root, text="Phone Number", font=("Times New Roman", 12), fg="#ADEFD1",
bg="#00203F").place(x=100, y=200)
phone_entry = tk.Entry(root, textvariable=phone, width=30, font=("times new roman", 12),
validate="key", validatecommand=(vcmd, "%P"))
phone_entry.place(x=250, y=200)
phone_entry.bind("<Return>", lambda e: focus_next(e, address_entry))
tk.Label(root, text="Address", font=("times new roman", 12), fg="#ADEFD1",
bg="#00203F").place(x=100, y=250)
address_entry = tk.Entry(root, textvariable=address, width=30, font=("times new roman",
12))
address_entry.place(x=250, y=250)
address_entry.bind("<Return>", lambda e: focus_next(e, register_button))
# Buttons
register_button = tk.Button(root, text="Register", command=register, **button_style)
register_button.place(x=100, y=300)
tk.Button(root, text="Search", command=search, **button_style).place(x=250, y=300)
tk.Button(root, text="Delete", command=delete, **button_style).place(x=400, y=300)
tk.Button(root, text="Refresh", command=refresh_data, **button_style).place(x=550, y=300)
# Define Treeview columns
columns = ("ID", "Owner", "Number", "Model", "Phone", "Address", "DateTime")
# Treeview Style
style = ttk.Style()
style.theme_use("default")
style.configure("Custom.Treeview",
background="#00203F",
foreground="#ADEFD1",
rowheight=25,
fieldbackground="#00203F",
font=("times new roman",12))
style.map("Custom.Treeview",
background=[("selected", "#cce6ff")],
foreground=[("selected", "#ADEFD1")])
# Treeview setup
tree = ttk.Treeview(root, columns=columns, show="headings", style="Custom.Treeview")
for col in columns:
tree.heading(col, text=col)
tree.column(col, anchor="center", width=130)
tree.place(x=100, y=370, width=1100, height=400)
# Run
refresh_data()
owner_entry.focus()
root.mainloop()
db_connection.py
import mysql.connector
def get_connection():
return mysql.connector.connect(
host="localhost",
user="root",
password="Your password", # replace with your actual password
database="vehicle_db"
)
vehicle_backend.py
import sqlite3
from datetime import datetime
# Connect to database (or create if not exists)
def connect():
conn = sqlite3.connect("vehicles.db")
cur = conn.cursor()
cur.execute("""
CREATE TABLE IF NOT EXISTS vehicle (
id INTEGER PRIMARY KEY AUTOINCREMENT,
owner TEXT NOT NULL,
number TEXT UNIQUE NOT NULL,
model TEXT NOT NULL,
phone TEXT NOT NULL,
address TEXT NOT NULL,
date TEXT NOT NULL
)
""")
conn.commit()
conn.close()
connect()
# Register a new vehicle
def register_vehicle(owner, number, model, phone, address):
try:
conn = sqlite3.connect("vehicles.db")
cur = conn.cursor()
cur.execute("""
INSERT INTO vehicle (owner, number, model, phone, address, date)
VALUES (?, ?, ?, ?, ?, ?)
""", (owner, number, model, phone, address, datetime.now().strftime("%Y-%m-%d
%H:%M:%S")))
conn.commit()
conn.close()
return "Vehicle registered successfully."
except sqlite3.IntegrityError:
return "Vehicle number already exists."
# Search vehicle by number
def search_vehicle(number):
conn = sqlite3.connect("vehicles.db")
cur = conn.cursor()
cur.execute("SELECT * FROM vehicle WHERE number = ?", (number,))
result = cur.fetchone()
conn.close()
return result
# Delete vehicle by number
def delete_vehicle(number):
conn = sqlite3.connect("vehicles.db")
cur = conn.cursor()
cur.execute("DELETE FROM vehicle WHERE number = ?", (number,))
conn.commit()
rows_deleted = cur.rowcount
conn.close()
if rows_deleted:
return "Vehicle deleted successfully."
else:
return "Vehicle not found."
# View all vehicles
def view_all_vehicles():
conn = sqlite3.connect("vehicles.db")
cur = conn.cursor()
cur.execute("SELECT * FROM vehicle ORDER BY id DESC")
rows = cur.fetchall()
conn.close()
return rows
Output:
Result:
Thus, The Vehicle Registration System was successfully developed using Python with
a MySQL backend. The application allows users to register vehicles, search and delete vehicle
records by number, and display all stored records through a graphical user interface. All
database operations such as insert, select, delete, and fetch were performed and validated
successfully.