Skip to content

Commit e88c3e6

Browse files
authored
Merge pull request #97 from michalszkil/feature/python-weather-app
Add Python Weather App
2 parents 6b6a848 + 543e55d commit e88c3e6

File tree

3 files changed

+106
-0
lines changed

3 files changed

+106
-0
lines changed

Python/Weather_App/README.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
## Python Weather App
2+
3+
A simple weather application built with Python's Tkinter library that fetches and displays current weather information for a specified city using the OpenWeatherMap API. The app also shows a static map of the city's location using the StaticMap library.
4+
5+
### Features
6+
- Fetches current weather data including temperature, humidity, and weather conditions.
7+
- Displays a static map of the city using the StaticMap library.
8+
- User-friendly GUI built with Tkinter.
9+
10+
### How to Run
11+
1. Clone the repository
12+
2. Install the required packages:
13+
```
14+
pip install -r requirements.txt
15+
```
16+
3. Replace `OPENWEATHER_API_KEY` in `weather_app.py` with your actual OpenWeatherMap API key.
17+
4. Run the application:
18+
```
19+
python weather_app.py
20+
```
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Pillow
2+
requests
3+
staticmap

Python/Weather_App/weather_app.py

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
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

Comments
 (0)