Build a Weather App in Python with OpenWeather API

Build a Weather App in Python with OpenWeather API

🌤️ Build a Weather App in Python with OpenWeather API

Level: Beginner
Tech used: Python, Tkinter, OpenWeatherMap API

🛠️ What You’ll Build

A simple desktop app that shows the current weather for any city using the OpenWeatherMap API and Python’s Tkinter GUI toolkit.

📦 Requirements

  • Python installed (3.x recommended)
  • Sign up for an API key at OpenWeatherMap
  • requests and tkinter modules

💻 Sample Code


import tkinter as tk
import requests

def get_weather():
    city = entry.get()
    api_key = "YOUR_API_KEY"  # Replace with your actual API key
    url = f"https://api.openweathermap.org/data/2.5/weather?q={city}&appid={api_key}&units=metric"
    response = requests.get(url)
    data = response.json()
    if data.get("main"):
        weather = data["weather"][0]["description"]
        temp = data["main"]["temp"]
        result.set(f"{city.title()} - {weather}, {temp}°C")
    else:
        result.set("City not found!")

root = tk.Tk()
root.title("Weather App")

entry = tk.Entry(root)
entry.pack()

tk.Button(root, text="Get Weather", command=get_weather).pack()
result = tk.StringVar()
tk.Label(root, textvariable=result).pack()

root.mainloop()
  

🎯 Why Learn This?

  • Practice working with APIs
  • Understand HTTP requests using requests
  • Design a basic GUI app using Tkinter

💡 Bonus Tips

You can enhance this project by showing weather icons, forecasts, or integrating it into a web dashboard using Flask.

📚 Related Resources

Affiliate Picks:
🛍️ Best Python Programming Books on Amazon

Comments