Open In App
Related Articles

Create a GUI for Weather Forecast using openweathermap API in Python

Improve Article
Save Article
Like Article

Prerequisites: Find current weather of any city using openweathermap API

The idea of this article is to provide a simple GUI application to users to get the current temperature of any city they wish to see. The system also provides a simple user interface for simplification of application. It also provides an amazing UX for its users. The features of this application will be that this will be a real-time weather forecast app that returns the current temperature, maximum and minimum temperature, humidity, latitude, and longitude coordinates of the searched city, current date, and time. It can also change its theme according to the time of day.

Openweathermap API

openweathermap is a service that provides weather data, including current weather data, forecasts, and historical data to the developers of web services and mobile applications.

Modules Needed

  • Tkinter: It is the fastest and easiest way to create GUI applications. This is included in the Python standard modules so there is no need to install it externally.
  • PIL: PIL is the Python Imaging Library which provides the python interpreter with image editing capabilities.
  • json: This module is used to handle JSON files and comes built in with Python. So there is no need to install it externally
  • requests: It is used for making HTTP requests to a specified URL. This module does not comes built in with Python. To install it type the below command in the terminal.
pip install requests

Images Used in the below application:




Below is the implementation.


# python3 -- Weather Application using API
# importing the libraries
from tkinter import *
import requests
import json
import datetime
from PIL import ImageTk, Image
# necessary details
root = Tk()
root.title("Weather App")
root['background'] = "white"
# Image
new = ImageTk.PhotoImage('logo.png'))
panel = Label(root, image=new), y=520)
# Dates
dt =
date = Label(root, text=dt.strftime('%A--'), bg='white', font=("bold", 15)), y=130)
month = Label(root, text=dt.strftime('%m %B'), bg='white', font=("bold", 15)), y=130)
# Time
hour = Label(root, text=dt.strftime('%I : %M %p'),
             bg='white', font=("bold", 15)), y=160)
# Theme for the respective time the application is used
if int((dt.strftime('%I'))) >= 8 & int((dt.strftime('%I'))) <= 5:
    img = ImageTk.PhotoImage('moon.png'))
    panel = Label(root, image=img), y=200)
    img = ImageTk.PhotoImage('sun.png'))
    panel = Label(root, image=img), y=200)
# City Search
city_name = StringVar()
city_entry = Entry(root, textvariable=city_name, width=45)
city_entry.grid(row=1, column=0, ipady=10, stick=W+E+N+S)
def city_name():
    # API Call
    api_request = requests.get(""
                               + city_entry.get() + "&units=metric&appid="+api_key)
    api = json.loads(api_request.content)
    # Temperatures
    y = api['main']
    current_temprature = y['temp']
    humidity = y['humidity']
    tempmin = y['temp_min']
    tempmax = y['temp_max']
    # Coordinates
    x = api['coord']
    longtitude = x['lon']
    latitude = x['lat']
    # Country
    z = api['sys']
    country = z['country']
    citi = api['name']
    # Adding the received info into the screen
# Search Bar and Button
city_nameButton = Button(root, text="Search", command=city_name)
city_nameButton.grid(row=1, column=1, padx=5, stick=W+E+N+S)
# Country  Names and Coordinates
lable_citi = Label(root, text="...", width=0
                   bg='white', font=("bold", 15)), y=63)
lable_country = Label(root, text="...", width=0
                      bg='white', font=("bold", 15)), y=63)
lable_lon = Label(root, text="...", width=0,
                  bg='white', font=("Helvetica", 15)), y=95)
lable_lat = Label(root, text="...", width=0,
                  bg='white', font=("Helvetica", 15)), y=95)
# Current Temperature
lable_temp = Label(root, text="...", width=0, bg='white',
                   font=("Helvetica", 110), fg='black'), y=220)
# Other temperature details
humi = Label(root, text="Humidity: ", width=0
             bg='white', font=("bold", 15)), y=400)
lable_humidity = Label(root, text="...", width=0,
                       bg='white', font=("bold", 15)), y=400)
maxi = Label(root, text="Max. Temp.: ", width=0
             bg='white', font=("bold", 15)), y=430)
max_temp = Label(root, text="...", width=0
                 bg='white', font=("bold", 15)), y=430)
mini = Label(root, text="Min. Temp.: ", width=0
             bg='white', font=("bold", 15)), y=460)
min_temp = Label(root, text="...", width=0
                 bg='white', font=("bold", 15)), y=460)
# Note
note = Label(root, text="All temperatures in degree celsius",
             bg='white', font=("italic", 10)), y=495)


Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Last Updated : 05 Sep, 2020
Like Article
Save Article
Similar Reads
Complete Tutorials