In our modern era, where weather conditions have a significant impact on our daily lives, having access to accurate and up-to-date weather information is crucial. A weather application provides users with real-time weather forecasts, current conditions, and other meteorological data to help them plan their activities accordingly. The weather app in Python leverages various tools and libraries to retrieve weather data from reliable sources and present it to users in a user-friendly manner.
Introduction
Our Weather App in Python provides real-time weather information for any location worldwide. With its user-friendly interface and accurate data, you can easily check current conditions, temperature, humidity, wind speed, and more. Python’s extensive range of libraries, such as requests, JSON, and matplotlib, allows developers to access weather APIs, process the received data, and display it using graphs, charts, or even a graphical user interface (GUI).
Objective
The objective is to enhance the user’s experience by delivering an efficient and comprehensive weather forecasting tool. Our Weather App in Python will provide users with a convenient and reliable way to access real-time weather information. Offering accurate data and a user-friendly interface, this aims to help users stay informed about the current conditions, temperature, humidity, and wind speed of any location worldwide.
Requirements
1. Basic Python Knowledge
2. VS Code or Any Text Editor
3. JSON library for accessing weather API. We have used OpenWeatherMap. You can use other APIs as per your choice.
Source Code
import requests import json import tkinter as tk from tkinter import messagebox def get_weather(): api_key = "9b0358eeda838827831dcdb7c160d14b" city=city_entry.get() url=f"http://api.openweathermap.org/data/2.5/weather?q={city}&appid={api_key}&units=metric" try: response=requests.get(url) data=json.loads(response.text) temperature=data["main"]["temp"] description=data["weather"][0]["description"] messagebox.showinfo("weather", f"temperature: {temperature}°C\nDescription:{description}") except requests.exceptions.RequestException: messagebox.showerror("Error", "Failed to retrieve weather data.") except KeyError: messagebox.showerror("Error", "Invalid response is received from the API.") window=tk.Tk()
Explanation of the Code
1. We begin the source code by importing the necessary modules: `requests` for making HTTP requests, `json` for working with JSON data, and `tkinter` for creating a graphical user interface.
2. We defined `get_weather()` function. This is responsible for retrieving and displaying the weather information.
3. Next, we specified the API key and city obtained from the user through a text entry widget in the graphical user interface.
4. The URL for the OpenWeatherMap API is constructed using the API key and the user-entered city. The URL specifies that the temperature should be returned in Celsius.
5. We used `try-except` block to handle any potential errors that may occur during the API request.
6. Inside the `try` block, an HTTP `GET` request is made to the API using the constructed URL. The response is obtained, and its JSON content is loaded into a Python dictionary using `json.loads()`.
7. The temperature and weather description is extracted from the JSON response using appropriate keys.
8. We created a Tkinter message box using `messagebox.showinfo()`. It displays the retrieved temperature and weather description.
9. If an error occurs during the API request (e.g., network issue or invalid city name), an exception of type `requests.exceptions.RequestException` is raised. In such cases, an error message is displayed using `messagebox.showerror()`.
10. Finally, we created a Tkinter window using `tk.Tk()`.
Output
Figure 1: Entering the city name into the input box
Figure 2: Retrieved the Temperature and Description using API Key
Figure 3: Detailed information on the weather of the city entered is obtained in the console
Conclusion
Hence, based on user input, we have successfully built a Weather App in Python using the OpenWeatherMap API. It displays the temperature and weather description in a Tkinter message box. Appropriate error messages are displayed if an error occurs during the API request, or the expected data is not found.

Sulagna is an experienced Python developer and developed web applications, data analysis tools, and automation scripts using Python. With several years of hands-on experience in Python development, she has gained a deep understanding of the language and its extensive ecosystem of libraries and frameworks. Sulagna has over 5 years of experince as a technical content writer/reviewer. She has contributed articles and tutorials to reputable online platforms, sharing her knowledge and insights on Python development best practices, web development, and data analysis.
0 Comments