Step-by-Step Tutorial on Creating a Weather App Using Python and Tkinter

Posted by


In this tutorial, we will build a simple weather app using Python and Tkinter. Tkinter is a standard GUI toolkit for Python, and it is widely used for building desktop applications. We will use the OpenWeatherMap API to fetch weather data and display it in our app.

Step 1: Set up your development environment

First, you need to have Python installed on your computer. You can download Python from the official website (https://www.python.org/downloads/). After installing Python, you need to install Tkinter and requests modules. You can do this by running the following commands in your terminal:

pip install tkinter
pip install requests

Step 2: Sign up for an OpenWeatherMap API key

Next, sign up for a free account on the OpenWeatherMap website (https://home.openweathermap.org/users/sign_up) to get an API key. This key will allow you to access weather data from the OpenWeatherMap API.

Step 3: Create a new Python file

Create a new Python file in your favorite text editor or IDE. Save the file as weather_app.py.

Step 4: Import the necessary modules

In your weather_app.py file, import the Tk class from the tkinter module and the requests module:

from tkinter import Tk, Label
import requests

Step 5: Create the main window

Create the main window of your app using the Tk class:

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

Step 6: Define a function to fetch weather data

Next, define a function called get_weather that will fetch weather data from the OpenWeatherMap API. You will need to replace 'YOUR_API_KEY' with your actual API key:

def get_weather():
    api_key = 'YOUR_API_KEY'
    url = 'http://api.openweathermap.org/data/2.5/weather?q=London&appid=' + api_key
    response = requests.get(url)
    data = response.json()

    weather = data['weather'][0]['description']
    temperature = data['main']['temp']
    humidity = data['main']['humidity']

    weather_label.config(text='Weather: ' + weather)
    temperature_label.config(text='Temperature: ' + str(temperature) + '°C')
    humidity_label.config(text='Humidity: ' + str(humidity) + '%')

Step 7: Create labels to display weather data

Create labels to display the weather, temperature, and humidity data in the main window:

weather_label = Label(root, font=('Arial', 14))
weather_label.pack()

temperature_label = Label(root, font=('Arial', 14))
temperature_label.pack()

humidity_label = Label(root, font=('Arial', 14))
humidity_label.pack()

Step 8: Create a button to fetch weather data

Create a button that will call the get_weather function when clicked:

refresh_btn = Button(root, text='Refresh', command=get_weather)
refresh_btn.pack()

Step 9: Run the main loop

Finally, run the main loop of your app:

get_weather()
root.mainloop()

That’s it! You have now built a simple weather app with Python and Tkinter. When you run the app, it will fetch weather data for London from the OpenWeatherMap API and display it in the main window. You can customize the app further by adding more features or by fetching weather data for different cities.