|
| 1 | +""" |
| 2 | +WeatherSentinel v1.0 - Enterprise Edition |
| 3 | +AI-Assisted Weather Prediction & Forecast Intelligence Tool |
| 4 | +Clean UI • Fast Predictions • Location-Based Forecasting |
| 5 | +""" |
| 6 | + |
| 7 | +import os, sys, threading, time, random |
| 8 | +import tkinter as tk |
| 9 | +from tkinter import messagebox, ttk |
| 10 | + |
| 11 | +import ttkbootstrap as tb |
| 12 | +from ttkbootstrap.constants import * |
| 13 | + |
| 14 | + |
| 15 | +# ---------------------- UTIL ---------------------- |
| 16 | +def resource_path(file_name): |
| 17 | + base_path = getattr(sys, "_MEIPASS", os.path.dirname(os.path.abspath(__file__))) |
| 18 | + return os.path.join(base_path, file_name) |
| 19 | + |
| 20 | + |
| 21 | +# ---------------------- WEATHER WORKER ---------------------- |
| 22 | +class WeatherWorker: |
| 23 | + def __init__(self, location, days, callbacks): |
| 24 | + self.location = location |
| 25 | + self.days = days |
| 26 | + self.callbacks = callbacks |
| 27 | + self._running = True |
| 28 | + |
| 29 | + def stop(self): |
| 30 | + self._running = False |
| 31 | + |
| 32 | + def run(self): |
| 33 | + forecasts = [] |
| 34 | + conditions = ["Sunny", "Cloudy", "Rain", "Storm", "Snow", "Fog"] |
| 35 | + |
| 36 | + for day in range(1, self.days + 1): |
| 37 | + if not self._running: |
| 38 | + break |
| 39 | + |
| 40 | + time.sleep(0.5) # simulate API delay |
| 41 | + |
| 42 | + forecast = { |
| 43 | + "day": f"Day {day}", |
| 44 | + "temp": random.randint(-5, 35), |
| 45 | + "humidity": random.randint(30, 90), |
| 46 | + "condition": random.choice(conditions) |
| 47 | + } |
| 48 | + forecasts.append(forecast) |
| 49 | + |
| 50 | + if "progress" in self.callbacks: |
| 51 | + self.callbacks["progress"](int(day / self.days * 100)) |
| 52 | + |
| 53 | + if "update" in self.callbacks: |
| 54 | + self.callbacks["update"](forecast) |
| 55 | + |
| 56 | + if "finished" in self.callbacks: |
| 57 | + self.callbacks["finished"](forecasts) |
| 58 | + |
| 59 | + |
| 60 | +# ---------------------- MAIN APP ---------------------- |
| 61 | +class WeatherSentinelApp: |
| 62 | + APP_NAME = "WeatherSentinel" |
| 63 | + APP_VERSION = "1.0" |
| 64 | + |
| 65 | + def __init__(self): |
| 66 | + self.root = tb.Window(themename="darkly") |
| 67 | + self.root.title(f"{self.APP_NAME} v{self.APP_VERSION}") |
| 68 | + self.root.minsize(900, 550) |
| 69 | + |
| 70 | + try: |
| 71 | + self.root.iconbitmap(resource_path("weather.ico")) |
| 72 | + except: |
| 73 | + pass |
| 74 | + |
| 75 | + self.worker = None |
| 76 | + self._build_ui() |
| 77 | + self._apply_styles() |
| 78 | + |
| 79 | + # ---------------------- UI ---------------------- |
| 80 | + def _build_ui(self): |
| 81 | + main = tb.Frame(self.root, padding=12) |
| 82 | + main.pack(fill=BOTH, expand=True) |
| 83 | + |
| 84 | + tb.Label( |
| 85 | + main, |
| 86 | + text=f"🌤️ {self.APP_NAME} - Weather Prediction System", |
| 87 | + font=("Segoe UI", 22, "bold") |
| 88 | + ).pack(pady=(0, 5)) |
| 89 | + |
| 90 | + tb.Label( |
| 91 | + main, |
| 92 | + text="Location-based weather forecasting with predictive insights", |
| 93 | + font=("Segoe UI", 10, "italic"), |
| 94 | + foreground="#9ca3af" |
| 95 | + ).pack(pady=(0, 20)) |
| 96 | + |
| 97 | + # Controls |
| 98 | + controls = tb.Frame(main) |
| 99 | + controls.pack(fill=X, pady=(0, 10)) |
| 100 | + |
| 101 | + tb.Label(controls, text="Location").pack(side=LEFT, padx=5) |
| 102 | + self.location_entry = tb.Entry(controls, width=30) |
| 103 | + self.location_entry.pack(side=LEFT, padx=5) |
| 104 | + self.location_entry.insert(0, "New York") |
| 105 | + |
| 106 | + tb.Label(controls, text="Forecast Days").pack(side=LEFT, padx=5) |
| 107 | + self.days_combo = tb.Combobox(controls, values=[1, 3, 5, 7], width=5) |
| 108 | + self.days_combo.set(5) |
| 109 | + self.days_combo.pack(side=LEFT, padx=5) |
| 110 | + |
| 111 | + self.start_btn = tb.Button( |
| 112 | + controls, text="🔍 Predict", bootstyle=SUCCESS, command=self.start |
| 113 | + ) |
| 114 | + self.start_btn.pack(side=LEFT, padx=5) |
| 115 | + |
| 116 | + self.cancel_btn = tb.Button( |
| 117 | + controls, text="⏹ Cancel", bootstyle=DANGER, command=self.cancel |
| 118 | + ) |
| 119 | + self.cancel_btn.pack(side=LEFT, padx=5) |
| 120 | + self.cancel_btn.config(state=DISABLED) |
| 121 | + |
| 122 | + # Progress |
| 123 | + self.progress = tb.Progressbar( |
| 124 | + main, bootstyle="info-striped", maximum=100 |
| 125 | + ) |
| 126 | + self.progress.pack(fill=X, pady=(0, 10)) |
| 127 | + |
| 128 | + # Results Table |
| 129 | + columns = ("day", "temp", "humidity", "condition") |
| 130 | + self.tree = ttk.Treeview( |
| 131 | + main, columns=columns, show="headings", height=15 |
| 132 | + ) |
| 133 | + for col in columns: |
| 134 | + self.tree.heading(col, text=col.capitalize()) |
| 135 | + self.tree.column(col, anchor=CENTER) |
| 136 | + |
| 137 | + self.tree.pack(fill=BOTH, expand=True) |
| 138 | + |
| 139 | + # Footer |
| 140 | + self.status_label = tb.Label( |
| 141 | + main, text="Ready", anchor=E |
| 142 | + ) |
| 143 | + self.status_label.pack(fill=X, pady=(6, 0)) |
| 144 | + |
| 145 | + # ---------------------- Actions ---------------------- |
| 146 | + def start(self): |
| 147 | + location = self.location_entry.get().strip() |
| 148 | + if not location: |
| 149 | + messagebox.showwarning("Input Required", "Please enter a location") |
| 150 | + return |
| 151 | + |
| 152 | + self.tree.delete(*self.tree.get_children()) |
| 153 | + self.progress["value"] = 0 |
| 154 | + self.start_btn.config(state=DISABLED) |
| 155 | + self.cancel_btn.config(state=NORMAL) |
| 156 | + self.status_label.config(text="Predicting weather...") |
| 157 | + |
| 158 | + self.worker = WeatherWorker( |
| 159 | + location=location, |
| 160 | + days=int(self.days_combo.get()), |
| 161 | + callbacks={ |
| 162 | + "progress": self.set_progress, |
| 163 | + "update": self.add_forecast, |
| 164 | + "finished": self.finish |
| 165 | + } |
| 166 | + ) |
| 167 | + |
| 168 | + threading.Thread(target=self.worker.run, daemon=True).start() |
| 169 | + |
| 170 | + def cancel(self): |
| 171 | + if self.worker: |
| 172 | + self.worker.stop() |
| 173 | + self.finish([]) |
| 174 | + |
| 175 | + def add_forecast(self, data): |
| 176 | + self.tree.insert( |
| 177 | + "", END, |
| 178 | + values=( |
| 179 | + data["day"], |
| 180 | + f"{data['temp']} °C", |
| 181 | + f"{data['humidity']} %", |
| 182 | + data["condition"] |
| 183 | + ) |
| 184 | + ) |
| 185 | + |
| 186 | + def set_progress(self, value): |
| 187 | + self.progress["value"] = value |
| 188 | + |
| 189 | + def finish(self, forecasts): |
| 190 | + self.start_btn.config(state=NORMAL) |
| 191 | + self.cancel_btn.config(state=DISABLED) |
| 192 | + self.status_label.config( |
| 193 | + text=f"Forecast completed ({len(forecasts)} days)" |
| 194 | + ) |
| 195 | + |
| 196 | + # ---------------------- Styles ---------------------- |
| 197 | + def _apply_styles(self): |
| 198 | + style = tb.Style() # get existing style |
| 199 | + style.configure( |
| 200 | + "TProgressbar", |
| 201 | + troughcolor="#1f2937", |
| 202 | + background="#38bdf8", |
| 203 | + thickness=14 |
| 204 | + ) |
| 205 | + |
| 206 | + # ---------------------- Run ---------------------- |
| 207 | + def run(self): |
| 208 | + self.root.mainloop() |
| 209 | + |
| 210 | + |
| 211 | +# ---------------------- RUN ---------------------- |
| 212 | +if __name__ == "__main__": |
| 213 | + app = WeatherSentinelApp() |
| 214 | + app.run() |
0 commit comments