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 instruction manual, we will learn how to create a Graphical User Interface (GUI) using Python programming language to extract vehicle information from a VIN number.

Prerequisites

Before we dive into the details of creating a GUI, you should have a basic understanding of Python programming, object-oriented programming (OOP) concepts, and how to work with the Tkinter module.

List of recommended settings

  • pip install tkinter, pandas, requests

  • It is expected that the user will have access to any standalone IDE such as VS-Code, PyCharm, Atom or Sublime text.

  • Even online Python compilers can also be used such as Kaggle.com, Google Cloud platform or any other will do.

  • Updated version of Python. At the time of writing the article I have used 3.10.9 version.

  • Knowledge of the use of Jupyter notebook.

  • Knowledge and application of virtual environment would be beneficial but not required also use of APIs in some context would be useful

Steps required to accomplish the task

Step 1: Import the necessary modules

from tkinter import*
import requests

Next, we need to import the necessary libraries in your Python script. In this case, we need the requests library for making API requests and the tkinter library for creating the GUI interface. Here's the import statement −

Step 2: Create a function to fetch VIN data

Now, you need to create a function that fetches the vehicle information from the VIN number using an API. In this example, we are using the VIN decoder API from NHTSA (National Highway Traffic Safety Administration) to fetch the vehicle information. Here's the function:

def get_vin_data(vin):
   url = f'https://vpic.nhtsa.dot.gov/api/vehicles/decodevin/{vin}?format=json'
   response = requests.get(url)
   if response.status_code == 200:
      return response.json()['Results']
   else:
      return None

This function takes a VIN number as input and returns the vehicle information as a JSON object.

Step 3: Create a GUI interface to input VIN and display vehicle information −

Next, you need to create a GUI interface that allows the user to input the VIN number and displays the vehicle information as output. In this example, we are using the tkinter library to create the GUI elements. Here's the code −

def display_vin_data():
   vin = vin_entry.get()
   data = get_vin_data(vin)
   if data:
      for d in data:
         info_text.insert(END, f"{d['Variable']} : {d['Value']}\n")
   else:
      info_text.insert(END, "No data found for the VIN provided.\n")

root = Tk()
root.title("VIN Decoder")

vin_label = Label(root, text="Enter VIN: ")
vin_label.pack()

vin_entry = Entry(root)
vin_entry.pack()

submit_button = Button(root, text="Decode", command=display_vin_data)
submit_button.pack()

info_text = Text(root)
info_text.pack()

root.mainloop()

This code creates a window with a label, text box, button, and another text box. The user can enter the VIN number in the text box and click on the "Decode" button to display the vehicle information in the second text box.

Step 4: Display the results as a technical document:

If you want to display the vehicle information as a technical document, you can modify the display_vin_data function to generate the document in the desired format. Here's the updated code

def display_vin_data():
   vin = vin_entry.get()
   data = get_vin_data(vin)
   if data:
      doc_text = f"Vehicle Information\n{'='*20}\n"
      for d in data:
         doc_text += f"{d['Variable']} : {d['Value']}\n"
      info_text.insert(END, doc_text)
   else:
      info_text.insert(END, "No data found for the VIN provided.\n")

This code generates a technical document in the following format

Vehicle Information
====================
<Variable 1> : <Value 1>
<Variable 2> : <Value 2>
...
<Variable n> : <Value n>

Final code, program

import requests
from tkinter import *

def get_vin_data(vin):
   url = f'https://vpic.nhtsa.dot.gov/api/vehicles/decodevin/{vin}?format=json'
   response = requests.get(url)
   if response.status_code == 200:
      return response.json()['Results']
   else:
      return None

def display_vin_data():
   vin = vin_entry.get()
   data = get_vin_data(vin)
   if data:
      doc_text = f"Vehicle Information\n{'='*20}\n"
      for d in data:
         doc_text += f"{d['Variable']} : {d['Value']}\n"
      info_text.insert(END, doc_text)
   else:
      info_text.insert(END, "No data found for the VIN provided.\n")

root = Tk()
root.title("VIN Decoder")

vin_label = Label(root, text="Enter VIN: ")
vin_label.pack()

vin_entry = Entry(root)
vin_entry.pack()

submit_button = Button(root, text="Decode", command=display_vin_data)
submit_button.pack()

info_text = Text(root)
info_text.pack()

root.mainloop() 

Output

Real World Examples

Example 1: Retrieving Vehicle Information from a VIN Number

  • User enters a VIN number into the input field (e.g. 1HGCM82633A004352)

  • User enters a valid API key into the input field

  • User clicks the "Submit" button

  • GUI displays the vehicle information retrieved from the API (e.g. 2003 Honda Accord)

Output

The screen asks for input of VIN from the user

Now we can see the output of the VIN as desired hence this proves that our app is working.

Example 2: Error Handling for Invalid VIN Number

  • User enters an invalid VIN number into the input field (e.g. 1234567890)

  • User enters a valid API key into the input field

  • User clicks the "Submit" button

  • GUI displays an error message indicating that the VIN number is invalid and identifies the error.

In the second scenario we are testing it for error checking as this is a quality control step which will enable us to check the validity of our output.

Example 3: Error Handling for Incorrect API Key

  • User enters a valid VIN number into the input field (e.g. JM1BK123451234567)

  • User enters an incorrect or invalid API key into the input field

  • User clicks the "Submit" button

  • GUI displays an error message indicating that there is something wrong with some of the digits of the VIN but tries its best to come up with the nearest possible out and thus proving that this can also be used to check for missing values and prediction analysis.

Conclusion

To use this script, save it as a Python file with a .py extension and run it using the Python interpreter. Once the GUI window opens, enter the VIN number in the text box and click on the "Decode" button to fetch and display the vehicle information as a technical document.

Creating a GUI to extract vehicle information from a VIN number using Python is a useful skill for anyone interested in the automotive industry or vehicle history. By following the steps outlined in this instruction manual, you can easily integrate this functionality into your own Python programs and applications. Whether you are a car enthusiast, mechanic, or vehicle historian, this tool can help you gain valuable insights into the vehicles you are interested in.

Note: This example uses the VIN decoder API from NHTSA, which has a limit of 25 requests per day for unregistered users. If you need to make more requests, you can register for an API key and use it in the URL.

Updated on: 25-Apr-2023

336 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements