Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Create a GUI to extract information from VIN number Using Python
A Vehicle Identification Number (VIN) is a unique 17-digit code assigned to every vehicle manufactured after 1981. It contains information about the vehicle's make, model, year of manufacture, country of origin, and other relevant details. In this tutorial, we will create a Graphical User Interface (GUI) using Python's Tkinter library to extract and display vehicle information from a VIN number.
Prerequisites
Before creating the GUI, you should have basic understanding of:
Python programming fundamentals
Tkinter module for GUI development
Making HTTP requests with the requests library
Python 3.x installed on your system
Required Libraries
Install the necessary libraries using pip ?
pip install requests
Note: Tkinter comes pre-installed with Python, so no separate installation is needed.
Step 1: Import Required Modules
First, import the necessary libraries for creating the GUI and making API requests ?
import requests from tkinter import * from tkinter import messagebox
Step 2: Create VIN Data Fetching Function
Create a function that fetches vehicle information using the NHTSA VIN decoder API ?
def get_vin_data(vin):
"""Fetch vehicle data from NHTSA API using VIN number"""
url = f'https://vpic.nhtsa.dot.gov/api/vehicles/decodevin/{vin}?format=json'
try:
response = requests.get(url, timeout=10)
if response.status_code == 200:
return response.json()['Results']
else:
return None
except requests.exceptions.RequestException as e:
print(f"Error fetching data: {e}")
return None
Step 3: Create GUI Interface
Build the main GUI window with input field, button, and display area ?
def display_vin_data():
"""Process VIN input and display vehicle information"""
vin = vin_entry.get().strip().upper()
# Clear previous results
info_text.delete(1.0, END)
# Validate VIN length
if len(vin) != 17:
messagebox.showerror("Invalid VIN", "VIN must be exactly 17 characters long")
return
# Fetch data
data = get_vin_data(vin)
if data:
doc_text = f"Vehicle Information for VIN: {vin}\n"
doc_text += "=" * 50 + "\n\n"
# Filter and display relevant information
relevant_fields = ['Make', 'Model', 'Model Year', 'Vehicle Type', 'Body Class',
'Engine Number of Cylinders', 'Fuel Type - Primary',
'Plant Country', 'Manufacturer Name']
for item in data:
if item['Variable'] in relevant_fields and item['Value']:
doc_text += f"{item['Variable']}: {item['Value']}\n"
info_text.insert(1.0, doc_text)
else:
info_text.insert(1.0, "Error: Could not retrieve data for the provided VIN.")
def clear_data():
"""Clear all input and output fields"""
vin_entry.delete(0, END)
info_text.delete(1.0, END)
# Create main window
root = Tk()
root.title("VIN Decoder - Vehicle Information Extractor")
root.geometry("600x500")
# VIN input section
input_frame = Frame(root)
input_frame.pack(pady=10)
vin_label = Label(input_frame, text="Enter VIN Number (17 digits):", font=("Arial", 12))
vin_label.pack()
vin_entry = Entry(input_frame, width=20, font=("Arial", 14))
vin_entry.pack(pady=5)
# Buttons
button_frame = Frame(root)
button_frame.pack(pady=10)
decode_button = Button(button_frame, text="Decode VIN", command=display_vin_data,
bg="lightblue", font=("Arial", 11))
decode_button.pack(side=LEFT, padx=5)
clear_button = Button(button_frame, text="Clear", command=clear_data,
bg="lightcoral", font=("Arial", 11))
clear_button.pack(side=LEFT, padx=5)
# Results display area
Label(root, text="Vehicle Information:", font=("Arial", 12, "bold")).pack(pady=(20,5))
info_text = Text(root, height=15, width=70, font=("Arial", 10))
info_text.pack(pady=5)
# Scrollbar for text area
scrollbar = Scrollbar(root)
scrollbar.pack(side=RIGHT, fill=Y)
info_text.config(yscrollcommand=scrollbar.set)
scrollbar.config(command=info_text.yview)
root.mainloop()
Complete VIN Decoder Application
Here's the complete code combining all components ?
import requests
from tkinter import *
from tkinter import messagebox
def get_vin_data(vin):
"""Fetch vehicle data from NHTSA API using VIN number"""
url = f'https://vpic.nhtsa.dot.gov/api/vehicles/decodevin/{vin}?format=json'
try:
response = requests.get(url, timeout=10)
if response.status_code == 200:
return response.json()['Results']
else:
return None
except requests.exceptions.RequestException as e:
print(f"Error fetching data: {e}")
return None
def display_vin_data():
"""Process VIN input and display vehicle information"""
vin = vin_entry.get().strip().upper()
# Clear previous results
info_text.delete(1.0, END)
# Validate VIN length
if len(vin) != 17:
messagebox.showerror("Invalid VIN", "VIN must be exactly 17 characters long")
return
# Fetch data
data = get_vin_data(vin)
if data:
doc_text = f"Vehicle Information for VIN: {vin}\n"
doc_text += "=" * 50 + "\n\n"
# Filter and display relevant information
relevant_fields = ['Make', 'Model', 'Model Year', 'Vehicle Type', 'Body Class',
'Engine Number of Cylinders', 'Fuel Type - Primary',
'Plant Country', 'Manufacturer Name']
for item in data:
if item['Variable'] in relevant_fields and item['Value']:
doc_text += f"{item['Variable']}: {item['Value']}\n"
info_text.insert(1.0, doc_text)
else:
info_text.insert(1.0, "Error: Could not retrieve data for the provided VIN.")
def clear_data():
"""Clear all input and output fields"""
vin_entry.delete(0, END)
info_text.delete(1.0, END)
# Create main window
root = Tk()
root.title("VIN Decoder - Vehicle Information Extractor")
root.geometry("600x500")
# VIN input section
input_frame = Frame(root)
input_frame.pack(pady=10)
vin_label = Label(input_frame, text="Enter VIN Number (17 digits):", font=("Arial", 12))
vin_label.pack()
vin_entry = Entry(input_frame, width=20, font=("Arial", 14))
vin_entry.pack(pady=5)
# Buttons
button_frame = Frame(root)
button_frame.pack(pady=10)
decode_button = Button(button_frame, text="Decode VIN", command=display_vin_data,
bg="lightblue", font=("Arial", 11))
decode_button.pack(side=LEFT, padx=5)
clear_button = Button(button_frame, text="Clear", command=clear_data,
bg="lightcoral", font=("Arial", 11))
clear_button.pack(side=LEFT, padx=5)
# Results display area
Label(root, text="Vehicle Information:", font=("Arial", 12, "bold")).pack(pady=(20,5))
info_text = Text(root, height=15, width=70, font=("Arial", 10))
info_text.pack(pady=5)
root.mainloop()
Key Features
VIN Validation: Ensures the entered VIN is exactly 17 characters long
Error Handling: Handles network errors and invalid VIN numbers gracefully
Filtered Results: Displays only relevant vehicle information fields
Clear Function: Allows users to clear input and output fields
User-Friendly Interface: Clean layout with proper fonts and spacing
Example Usage
To test the application, you can use sample VIN numbers like:
1HGCM82633A004352- Honda AccordJM1BK32F781234567- Mazda vehicleWVWZZZ1JZ3W386752- Volkswagen vehicle
Conclusion
This VIN decoder GUI application demonstrates how to combine Python's Tkinter library with web APIs to create useful tools. The application validates input, handles errors gracefully, and presents vehicle information in a user-friendly format. This approach can be extended to include additional features like saving results to files or integrating with databases for vehicle history tracking.
