DEV Community

Mate Technologies
Mate Technologies

Posted on

๐Ÿš— Build an AI-Powered Car Price Prediction GUI with Python (Step-by-Step)

In this tutorial, weโ€™ll build AutoValue Pro, a desktop application that predicts used car prices using Machine Learning and a modern Python GUI.

By the end, youโ€™ll learn how to:

Use Linear Regression for predictions

Build a desktop GUI with Tkinter + ttkbootstrap

Run ML tasks in a background thread

Display results with a progress bar

๐Ÿ“Œ Project Source Code (GitHub):
https://github.com/rogers-cyber/python-tiny-tools/tree/main/Car-price-prediction-GUI

๐Ÿง  What Weโ€™re Building

AutoValue Pro v1.0

AI-powered used car price predictor

Desktop GUI

Inputs:

Number of owners

Kilometers driven

Manufacturing year

๐Ÿ› ๏ธ Step 1: Install Required Libraries

Make sure Python 3.9+ is installed, then run:

pip install numpy scikit-learn ttkbootstrap
Enter fullscreen mode Exit fullscreen mode

Why these libraries?

numpy โ†’ numerical operations

scikit-learn โ†’ machine learning model

ttkbootstrap โ†’ modern Tkinter UI styling

๐Ÿ“‚ Step 2: Import Dependencies

Start by importing all required modules.

import os
import sys
import threading
import tkinter as tk
from tkinter import messagebox

import ttkbootstrap as tb
from ttkbootstrap.constants import *

import numpy as np
from sklearn.linear_model import LinearRegression
Enter fullscreen mode Exit fullscreen mode

Explanation:

threading keeps the UI responsive

tkinter builds the GUI

LinearRegression powers our AI model

๐Ÿ“ฆ Step 3: Handle File Paths (Utility Function)

This helps when packaging the app later (e.g., with PyInstaller).

def resource_path(file_name):
    base_path = getattr(sys, "_MEIPASS", os.path.dirname(os.path.abspath(__file__)))
    return os.path.join(base_path, file_name)
Enter fullscreen mode Exit fullscreen mode

โœ… This ensures icons and assets load correctly.

๐Ÿค– Step 4: Create the Machine Learning Worker

We separate ML logic from the UI using a worker class.

class PredictionWorker:
    def __init__(self, features, callbacks):
        self.features = features
        self.callbacks = callbacks
        self._running = True
Enter fullscreen mode Exit fullscreen mode

Why?

Keeps the GUI smooth

Makes the code cleaner and scalable

๐Ÿ“Š Step 4.1: Train a Demo Model

We train a simple Linear Regression model using sample data.

self.model = LinearRegression()

X = np.array([
    [1, 5000, 2015],
    [2, 3000, 2018],
    [3, 2000, 2020],
    [1, 8000, 2012],
    [2, 4000, 2016]
])

y = np.array([12000, 15000, 18000, 9000, 14000])

self.model.fit(X, y)
Enter fullscreen mode Exit fullscreen mode

๐Ÿ“Œ Features used:

Number of owners

Kilometers driven

Manufacturing year

โ–ถ๏ธ Step 4.2: Run the Prediction

def run(self):
    try:
        prediction = self.model.predict([self.features])[0]

        if not self._running:
            return

        if "result" in self.callbacks:
            self.callbacks["result"](round(prediction, 2))

    except Exception as e:
        if "error" in self.callbacks:
            self.callbacks["error"](str(e))
Enter fullscreen mode Exit fullscreen mode

โœ” Predicts price
โœ” Sends result back to the UI

๐Ÿ–ฅ๏ธ Step 5: Create the Main GUI App

class AutoValueApp:
    APP_NAME = "AutoValue Pro"
    APP_VERSION = "1.0"
Enter fullscreen mode Exit fullscreen mode

We initialize the main window:

self.root = tb.Window(themename="darkly")
self.root.title("AutoValue Pro v1.0")
self.root.minsize(900, 550)
Enter fullscreen mode Exit fullscreen mode

๐ŸŽจ darkly gives us a modern dark theme.

๐Ÿงพ Step 6: Build the User Interface
Title & Subtitle

tb.Label(
    main,
    text="๐Ÿš— AutoValue Pro โ€“ Car Price Predictor",
    font=("Segoe UI", 22, "bold")
).pack()
Enter fullscreen mode Exit fullscreen mode

๐Ÿงฎ Input Fields

self.owner_var = tk.IntVar(value=1)
self.km_var = tk.IntVar(value=3000)
self.year_var = tk.IntVar(value=2018)

tb.Label(form, text="Number of Owners").grid(row=0, column=0)
tb.Spinbox(form, from_=1, to=5, textvariable=self.owner_var).grid(row=0, column=1)
Enter fullscreen mode Exit fullscreen mode

๐Ÿ“Œ We repeat this pattern for KM driven and year.

๐Ÿ”˜ Step 7: Buttons (Predict & Cancel)

self.predict_btn = tb.Button(
    btns,
    text="๐Ÿ”ฎ Predict Price",
    bootstyle=SUCCESS,
    command=self.start_prediction
)

self.cancel_btn = tb.Button(
    btns,
    text="โน Cancel",
    bootstyle=DANGER,
    state=DISABLED,
    command=self.cancel
)

Enter fullscreen mode Exit fullscreen mode

โณ Step 8: Progress Bar Animation

self.progress = tb.Progressbar(
    main,
    maximum=100,
    bootstyle="success-striped"
)

def animate_progress(self):
    if self.smooth_value < self.target_progress:
        self.smooth_value += 1
        self.progress["value"] = self.smooth_value
    self.root.after(15, self.animate_progress)
Enter fullscreen mode Exit fullscreen mode

๐ŸŽฏ This creates a smooth animated loading effect.

๐Ÿ’ฐ Step 9: Display Prediction Result

def show_result(self, price):
    self.result_label.config(
        text=f"Predicted Price: ${price:,}"
    )
Enter fullscreen mode Exit fullscreen mode

โœ” Formats numbers nicely
โœ” Updates UI safely from worker callbacks

โ–ถ๏ธ Step 10: Run the Application

if __name__ == "__main__":
    app = AutoValueApp()
    app.run()
Enter fullscreen mode Exit fullscreen mode

๐Ÿš€ Your AI-powered desktop app is now live!

๐ŸŽ‰ Final Thoughts

Youโ€™ve learned how to:

Combine Machine Learning + GUI

Use threads to keep apps responsive

Build a clean, professional Python desktop app

๐Ÿ“‚ Full Project on GitHub:
https://github.com/rogers-cyber/python-tiny-tools/tree/main/Car-price-prediction-GUI

Top comments (0)