Skip to main content

How to Design a Location Tracking Module for Desktop Business Systems Using Python?

ttkbootstrap Mapview
This tutorial will show you how to create an interactive map application that allows users to input geographic coordinates and visualize locations on an interactive map using Python's Tkinter GUI framework enhanced with TTKBootstrap styling and the TkinterMapView widget. 

Preliminary 
Before I begin, it is recommended to activate the virtual environment before installing the relevant dependencies.
python -m venv venv
venv\Scripts\activate   
pip install ttkbootstrap tkintermapview
As usual, we need to import the relevant module and set the root values
from tkinter import *
import ttkbootstrap as tb
import tkintermapview
from ttkbootstrap.dialogs import Messagebox

root = tb.Window(themename='darkly')
root.title('Find My Map')
root.geometry("1200x1200")
I would like to divide it into three sections. 

a) Input Panel Layout, 
The headers are mainly a label with a map emoji. 
The main layout contains labels and input fields for latitude and longitude, followed by 2 buttons: set position and clear entries. They are set on the same row for easy navigation.

Why am I?

# Main header label for the application
header_label = tb.Label(root, text="Where Am I? 🗺️", font=("Helvetica", 24), 
                        bootstyle="success-inverse")
header_label.pack(pady=10)

# Create a frame to contain coordinate input controls
My_frame = tb.Frame(root, bootstyle="info")
My_frame.pack(pady=10, padx=30, fill="x", expand=False)

# Configure grid column weights for equal width distribution across 5 columns
for i in range(6):  # 6 columns (0-5)
    My_frame.columnconfigure(i, weight=1)

# Define a consistent font for input controls
my_font = ("Roboto", 12)

# Latitude label and input field
my_vertical_cordinate = tb.Label(My_frame, text="Latitude:", 
                                 font=my_font,
                                 bootstyle="info-inverse")
my_vertical_cordinate.grid(row=0, column=0, pady=5, padx=5, sticky=EW)

latitude_entry = tb.Entry(My_frame, bootstyle="info")
latitude_entry.grid(row=0, column=1, pady=5, padx=5, sticky=EW)

# Longitude label and input field
my_horizontal_cordinate = tb.Label(My_frame, text="Longitude:",
                                   font=my_font,
                                   bootstyle="info-inverse")
my_horizontal_cordinate.grid(row=0, column=2, pady=5, padx=5, sticky=EW)

longitude_entry = tb.Entry(My_frame)
longitude_entry.grid(row=0, column=3, pady=5, padx=5, sticky=EW)

# Button to set map position using entered coordinates
# Converts string inputs to floats and calls set_position method
set_position_button = tb.Button(My_frame, text="Set Position", 
                                bootstyle=("danger", "outline"),
                                command=set_map_position)
set_position_button.grid(row=0, column=4, pady=5, padx=5, sticky=EW)

# Button to clear the latitude and longitude entry fields
clear_button = tb.Button(My_frame, text="Clear Entries", 
                         bootstyle=("warning", "outline"),
                         command=clear_entries)
clear_button.grid(row=0, column=5, pady=5, padx=5, sticky=EW)
b) Mapview 
Layout 
Basically, a labelframe from Tkinter is required to display the map frame.

Map

# LabelFrame container for the map widget
my_label = LabelFrame(root, text="My map")
my_label.pack(pady=20, padx=20, fill="both", expand=True)

# Create map widget with specified dimensions and no rounded corners
map_widget = tkintermapview.TkinterMapView(my_label, width=1100, 
                                           height=1100, corner_radius=0)
map_widget.pack()

# Set initial zoom level (15 is a moderately close view)
map_widget.set_zoom(15)
c) The Main Function
  • Accepts latitude and longitude values through input fields
  • Validates numeric input format
  • Shows a scrollable/zoomable world map using OpenStreetMap tiles

  • Centers the map on specified coordinates
  • Places a marker at the target location

  • Instantly jumps to any location worldwide

  • Reset input fields for new search

Show error
def set_map_position():
    try:
        # Attempt to get and convert latitude input to a floating-point number
        lat = float(latitude_entry.get())
        # Attempt to get and convert longitude input to a floating-point number  
        lon = float(longitude_entry.get())
        # Center the map widget on the specified coordinates
        map_widget.set_position(lat, lon)     
    except ValueError:
        # This block executes if float() conversion fails (non-numeric input)
        # Display an error dialog box to inform the user
        Messagebox.show_error(
            title="Invalid coordinates.",  # Dialog title
            message="Please enter numeric values!!!"  # Error message
        )  
        # Clear the input fields after user acknowledges the error
        clear_entries()

def clear_entries():
       # Delete all text from latitude entry field (from position 0 to END)
       latitude_entry.delete(0, END)
       # Delete all text from longitude entry field (from position 0 to END)
       longitude_entry.delete(0, END)
Final Wrap-Up

This completes our tutorial on building a geographic MapViewer application with Python. We've successfully created a functional desktop application that transforms numerical coordinates into visual map locations through an intuitive graphical interface.

To view the full code, please click here

Comments

Popular Posts

How to build a Desktop Business System for Audiobook Workflow Management with Python?

In this tutorial, we will build a simple audiobook player using Python and CustomTkinter. You will learn how to convert text into speech using gTTS and play it with PyGame. We will also implement play, pause, and stop controls, like those in a real audio player. By the end, you will have a clean and functional desktop audiobook app. Preliminary   Before I begin, it is recommended to activate the virtual environment before installing the relevant dependencies. python -m venv venv venv\Scripts\activate pip install customtkinter pillow gTTS pygame CTkMessagebox pypdf Then, the following steps include setting up the file structure, app.py, and two additional folders: the uploads and media folders. The media folder contains the icons necessary to build the app; there are read, pause, and stop icons, as shown on the diagram. Step 1: Build up the app interface I have 4 sections here: the header, upload area, text body, and button group.  (a) The header—this section is basicall...

How to Build a Contact Form for SME Systems with Flask CKEditor (Part 1)

In this tutorial, we will be integrating Flask-WTF and Flask-CKEditor into your Flask application! You'll learn how to set up the editor, securely handle the formatted HTML content, and create a seamless user experience that enhances any project requiring user-generated content. Let's get started! Preliminary Before I begin, it is recommended to activate the virtual environment before installing the relevant dependencies. python -m venv venv venv\Scripts\activate pip install flask flask-wtf flask-ckeditor The next step is to set up an app structure; they are app.py, form.py, and the templates folder. Within the templates folder is contact.html. The app structure is described in the following diagram: Step 1: Create the basic minimum file configuration Let us set up the app.py by importing the necessary module and configuration. from flask import Flask, render_template app = Flask(__name__) app.config['SECRET_KEY'] = 'mysecretkey' @app.route('/...