|
| 1 | +import threading |
| 2 | +import webbrowser |
| 3 | +import tkinter as tk |
| 4 | +from dataclasses import dataclass |
| 5 | +from typing import List, Tuple |
| 6 | +import time |
| 7 | +import random |
| 8 | +import math |
| 9 | + |
| 10 | +import ttkbootstrap as tb |
| 11 | +from ttkbootstrap.constants import * |
| 12 | +from ttkbootstrap.widgets.scrolled import ScrolledText |
| 13 | + |
| 14 | +# ---------------- CONFIG ---------------- # |
| 15 | +UPDATE_INTERVAL = 2 # seconds |
| 16 | +START_LAT = 37.7749 # San Francisco (example) |
| 17 | +START_LON = -122.4194 |
| 18 | + |
| 19 | +# ---------------- GLOBAL STATE ---------------- # |
| 20 | +tracking_active = False |
| 21 | +location_history: List["Location"] = [] |
| 22 | + |
| 23 | +# ---------------- DATA STRUCTURE ---------------- # |
| 24 | +@dataclass(frozen=True) |
| 25 | +class Location: |
| 26 | + latitude: float |
| 27 | + longitude: float |
| 28 | + timestamp: str |
| 29 | + |
| 30 | +# ---------------- GPS SIMULATION ---------------- # |
| 31 | +def generate_next_location(lat: float, lon: float) -> Tuple[float, float]: |
| 32 | + delta = 0.0005 |
| 33 | + lat += random.uniform(-delta, delta) |
| 34 | + lon += random.uniform(-delta, delta) |
| 35 | + return round(lat, 6), round(lon, 6) |
| 36 | + |
| 37 | +# ---------------- TRACKING THREAD ---------------- # |
| 38 | +def tracking_loop(): |
| 39 | + global tracking_active |
| 40 | + lat, lon = START_LAT, START_LON |
| 41 | + |
| 42 | + while tracking_active: |
| 43 | + lat, lon = generate_next_location(lat, lon) |
| 44 | + timestamp = time.strftime("%Y-%m-%d %H:%M:%S") |
| 45 | + loc = Location(lat, lon, timestamp) |
| 46 | + location_history.append(loc) |
| 47 | + |
| 48 | + app.after(0, lambda l=loc: display_location(l)) |
| 49 | + time.sleep(UPDATE_INTERVAL) |
| 50 | + |
| 51 | +# ---------------- UI HELPERS ---------------- # |
| 52 | +def display_location(location: Location): |
| 53 | + text.configure(state="normal") |
| 54 | + text.insert( |
| 55 | + "end", |
| 56 | + f"📍 {location.timestamp}\nLatitude: {location.latitude}\nLongitude: {location.longitude}\n\n" |
| 57 | + ) |
| 58 | + text.see("end") |
| 59 | + text.configure(state="disabled") |
| 60 | + |
| 61 | +def open_in_maps(): |
| 62 | + if not location_history: |
| 63 | + return |
| 64 | + loc = location_history[-1] |
| 65 | + url = f"https://www.google.com/maps?q={loc.latitude},{loc.longitude}" |
| 66 | + webbrowser.open_new_tab(url) |
| 67 | + |
| 68 | +# ---------------- CONTROL FUNCTIONS ---------------- # |
| 69 | +def start_tracking(): |
| 70 | + global tracking_active |
| 71 | + if tracking_active: |
| 72 | + return |
| 73 | + tracking_active = True |
| 74 | + status_label.config(text="🟢 Tracking Active", bootstyle="success") |
| 75 | + threading.Thread(target=tracking_loop, daemon=True).start() |
| 76 | + |
| 77 | +def stop_tracking(): |
| 78 | + global tracking_active |
| 79 | + tracking_active = False |
| 80 | + status_label.config(text="🔴 Tracking Stopped", bootstyle="danger") |
| 81 | + |
| 82 | +def clear_history(): |
| 83 | + location_history.clear() |
| 84 | + text.configure(state="normal") |
| 85 | + text.delete("1.0", "end") |
| 86 | + text.configure(state="disabled") |
| 87 | + |
| 88 | +# ---------------- UI SETUP ---------------- # |
| 89 | +app = tb.Window( |
| 90 | + title="Real-Time GPS Tracking System", |
| 91 | + themename="darkly", |
| 92 | + size=(800, 600), |
| 93 | + resizable=(True, True), |
| 94 | +) |
| 95 | + |
| 96 | +top = tb.Frame(app, padding=15) |
| 97 | +top.pack(fill=tk.X) |
| 98 | + |
| 99 | +tb.Label( |
| 100 | + top, |
| 101 | + text="📡 Real-Time GPS Tracking System", |
| 102 | + font=("Segoe UI", 18, "bold"), |
| 103 | +).pack(anchor=tk.W) |
| 104 | + |
| 105 | +status_label = tb.Label(top, text="🔴 Tracking Stopped", bootstyle="danger") |
| 106 | +status_label.pack(anchor=tk.W, pady=5) |
| 107 | + |
| 108 | +btn_frame = tb.Frame(top) |
| 109 | +btn_frame.pack(fill=tk.X, pady=5) |
| 110 | + |
| 111 | +tb.Button(btn_frame, text="▶ Start Tracking", bootstyle="success", command=start_tracking).pack(side=tk.LEFT, padx=5) |
| 112 | +tb.Button(btn_frame, text="⏹ Stop Tracking", bootstyle="danger", command=stop_tracking).pack(side=tk.LEFT, padx=5) |
| 113 | +tb.Button(btn_frame, text="🗺 Open in Maps", bootstyle="info", command=open_in_maps).pack(side=tk.LEFT, padx=5) |
| 114 | +tb.Button(btn_frame, text="🧹 Clear", bootstyle="warning", command=clear_history).pack(side=tk.LEFT, padx=5) |
| 115 | + |
| 116 | +# ---------------- RESULTS ---------------- # |
| 117 | +result_frame = tb.Frame(app) |
| 118 | +result_frame.pack(fill=tk.BOTH, expand=True) |
| 119 | + |
| 120 | +result_box = ScrolledText(result_frame) |
| 121 | +result_box.pack(fill=tk.BOTH, expand=True) |
| 122 | + |
| 123 | +text = result_box.text |
| 124 | +text.configure(state="disabled", wrap="word") |
| 125 | + |
| 126 | +app.mainloop() |
0 commit comments