π― Project Objective
Build a Weather Application that fetches real-time weather data (temperature, humidity, conditions, etc.) for any city using a public weather API and displays it through a GUI interface.
Skills Demonstrated:
- Consuming REST APIs using
requests - Parsing JSON data
- GUI development using
Tkinter - Error handling and user input validation
π§ Project Overview
The Weather App allows users to:
- Enter a city name
- Fetch live weather information
- Display details such as temperature, humidity, wind speed, and weather description
Use Cases:
- Travel and outdoor planning
- Dashboard integration for smart homes
- Mobile or web weather widgets
π§© Example Code β Weather App (Tkinter + API)
Note: This code automatically installs missing modules like
requests.
import os
import sys
import subprocess
# Auto-install missing modules
def install(package):
try:
__import__(package)
except ImportError:
subprocess.check_call([sys.executable, "-m", "pip", "install", package])
install("requests")
install("tkinter")
import requests
import tkinter as tk
from tkinter import messagebox
# Replace with your OpenWeatherMap API key
API_KEY = "your_api_key_here" # Get one free from https://openweathermap.org/api
def get_weather():
city = city_entry.get()
if not city:
messagebox.showwarning("Input Error", "Please enter a city name.")
return
url = f"https://api.openweathermap.org/data/2.5/weather?q={city}&appid={API_KEY}&units=metric"
try:
response = requests.get(url)
data = response.json()
if data["cod"] != 200:
messagebox.showerror("Error", f"City not found: {city}")
return
weather = data["weather"][0]["description"].capitalize()
temperature = data["main"]["temp"]
humidity = data["main"]["humidity"]
wind_speed = data["wind"]["speed"]
result_label.config(text=f"π€οΈ {weather}\nπ‘οΈ Temperature: {temperature}Β°C\nπ§ Humidity: {humidity}%\nπ¬οΈ Wind Speed: {wind_speed} m/s")
except Exception as e:
messagebox.showerror("Error", f"Failed to retrieve weather: {e}")
# GUI Setup
root = tk.Tk()
root.title("Weather App")
root.geometry("400x300")
root.resizable(False, False)
tk.Label(root, text="Enter City Name:", font=("Arial", 12)).pack(pady=10)
city_entry = tk.Entry(root, width=30, font=("Arial", 12))
city_entry.pack()
tk.Button(root, text="Get Weather", command=get_weather, font=("Arial", 12), bg="#4CAF50", fg="white").pack(pady=10)
result_label = tk.Label(root, text="", font=("Arial", 12), justify="center")
result_label.pack(pady=20)
root.mainloop()
π§© Sample Output
Input:
City: London
Output (GUI):
π€οΈ Weather: Clear sky
π‘οΈ Temperature: 15Β°C
π§ Humidity: 65%
π¬οΈ Wind Speed: 3.5 m/s
βοΈ How It Works
- The app sends an API request to OpenWeatherMap.
- The server returns a JSON response with weather details.
- The app extracts relevant info and displays it neatly.
π API Setup Instructions
- Visit https://openweathermap.org/api
- Sign up (free) and get your API Key
- Replace the placeholder in the code:
API_KEY = "your_api_key_here" - Run the script β youβre good to go! π
π‘ Advanced Features to Try
- π Add weather icons (sun, cloud, rain, etc.)
- π Display local time of the city
- π Show 7-day forecast
- π Use geolocation to auto-detect current city
- πΎ Save recent search history
β Key Features
- Real-time API data
- Error handling for invalid city names
- Clean and responsive GUI
- Auto-module installation
- Extensible for mobile/web platforms
π Learning Outcomes
Handling dynamic data in GUI applications
API request and response handling
Parsing JSON with Python
Building user-friendly Tkinter interfaces

Leave a Reply