|
| 1 | +import tkinter as tk |
| 2 | +from tkinter import messagebox |
| 3 | +from PIL import Image, ImageTk |
| 4 | +import requests |
| 5 | +from io import BytesIO |
| 6 | +from staticmap import StaticMap, CircleMarker |
| 7 | + |
| 8 | +def get_weather() -> None: |
| 9 | + """ |
| 10 | + Fetch weather data for the specified city and update the UI with weather info and map. |
| 11 | + Arguments: |
| 12 | + None |
| 13 | + Returns: |
| 14 | + None |
| 15 | + """ |
| 16 | + city = city_entry.get() |
| 17 | + if not city: |
| 18 | + messagebox.showwarning("Input Error", "Please enter a city name") |
| 19 | + return |
| 20 | + |
| 21 | + api_key = "OPENWEATHER_API_KEY" # Replace with your OpenWeatherMap API key |
| 22 | + weather_url = "http://api.openweathermap.org/data/2.5/weather" |
| 23 | + params = {"q": city, "appid": api_key, "units": "metric"} |
| 24 | + |
| 25 | + try: |
| 26 | + response = requests.get(weather_url, params=params) # Fetch weather data |
| 27 | + data = response.json() |
| 28 | + if data["cod"] != 200: |
| 29 | + messagebox.showerror("Error", f"City not found: {city}") |
| 30 | + return |
| 31 | + |
| 32 | + city_name = data["name"] |
| 33 | + country = data["sys"]["country"] |
| 34 | + temp = data["main"]["temp"] |
| 35 | + weather_desc = data["weather"][0]["description"] |
| 36 | + humidity = data["main"]["humidity"] |
| 37 | + lat = data["coord"]["lat"] |
| 38 | + lon = data["coord"]["lon"] |
| 39 | + |
| 40 | + weather_info = ( |
| 41 | + f"City: {city_name}, {country}\n" |
| 42 | + f"Temperature: {temp}°C\n" |
| 43 | + f"Weather: {weather_desc.title()}\n" |
| 44 | + f"Humidity: {humidity}%" |
| 45 | + ) |
| 46 | + weather_label.config(text=weather_info) |
| 47 | + |
| 48 | + # Generate static map locally using OSM tiles |
| 49 | + m = StaticMap(450, 300, url_template='https://tile.openstreetmap.org/{z}/{x}/{y}.png') |
| 50 | + marker = CircleMarker((lon, lat), 'red', 12) |
| 51 | + m.add_marker(marker) |
| 52 | + image = m.render(zoom=10) |
| 53 | + |
| 54 | + # Convert to Tkinter image |
| 55 | + img_bytes = BytesIO() |
| 56 | + image.save(img_bytes, format='PNG') |
| 57 | + img_bytes.seek(0) |
| 58 | + map_photo = ImageTk.PhotoImage(Image.open(img_bytes)) |
| 59 | + |
| 60 | + map_label.config(image=map_photo) |
| 61 | + map_label.image = map_photo |
| 62 | + |
| 63 | + except Exception as e: |
| 64 | + messagebox.showerror("Error", f"Something went wrong:\n{e}") |
| 65 | + |
| 66 | +# Tkinter UI setup |
| 67 | +root = tk.Tk() |
| 68 | +root.title("Weather App with Local Map") |
| 69 | +root.geometry("500x600") |
| 70 | +root.resizable(False, False) |
| 71 | + |
| 72 | +# UI Components |
| 73 | +tk.Label(root, text="Enter City Name:", font=("Arial", 14)).pack(pady=10) |
| 74 | +city_entry = tk.Entry(root, font=("Arial", 14)) |
| 75 | +city_entry.pack(pady=5) |
| 76 | + |
| 77 | +tk.Button(root, text="Get Weather", font=("Arial", 14), command=get_weather).pack(pady=10) |
| 78 | +weather_label = tk.Label(root, text="", font=("Arial", 12), justify="left") |
| 79 | +weather_label.pack(pady=10) |
| 80 | +map_label = tk.Label(root) |
| 81 | +map_label.pack(pady=10) |
| 82 | + |
| 83 | +root.mainloop() |
0 commit comments