
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.
# 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.
# 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
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-UpThis 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.



Comments
Post a Comment