Weather script using OpenWeatherMap in Python with GUI

In this post, we will learn how to create a weather script using Python. We will use the Python OpenWeatherMap API (PyOWM) to get the weather details. At the end of the post, you will know how incredibly easy it is to get or forecast weather data in Python. The GUI we’ll be using here is Tkinter, but feel free to do this in your favorite GUI. If you need a refresher course in Tkinter, please go through the below blog post:
Introduction to Tkinter module in Python

So let’s get started!

Importing libraries for our Python weather script

We only need two libraries for this program:
pyowm and tkinter.

import pyowm
from tkinter import *

Make sure you have the pyowm library installed before you run this.

Creating our GUI window for the weather script

Let’s start by deciding how we want our GUI to look.

Create the GUI window using the Tk() function and set the window name.

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

Next, set the background color and the dimensions of the window.

root.configure(background = "#a1dbcd")
root.geometry("500x480")

Now, let’s put in the labels for our desired fields.
Here, we take a city name input from the user and will output the temperature, humidity and a brief description of the weather. You can add more fields if you want. The pyowm library has a huge array of weather data for a particular city to select from.

label = Label(root, text = "Weather Script", fg = '#a1dbcd', bg = '#383a39')     #This is the head label
label1 = Label(root, text = "Enter the City :", fg = 'black', bg = '#a1dbcd')
label2 = Label(root, text = "Temperature :", fg = 'black', bg = '#a1dbcd')
label3 = Label(root, text = "Humidity :",fg = 'black', bg = '#a1dbcd')
label4 = Label(root, text = "Description  :",  fg = 'black', bg = '#a1dbcd')

We will now create text-box fields to take city input and output temperature, humidity, and description data. We use the Entry widget to provide a single-line text-box for this purpose.

city_f = Entry(root)
temp_f = Entry(root)
humid_f = Entry(root)
desc_f = Entry(root)

Next, we create two buttons: “Tell Weather” and “Clear”.
On clicking the “Tell Weather” button it executes the function omw().
And, on clicking the “Clear” button it executes the function clear().
Both these functions are explained in detail later in the post.

b1 = Button(root, text = "Tell Weather!", bg = '#383a39',fg = '#a1dbcd', command = omw)
b2 = Button(root, text = "Clear", bg = '#383a39', fg = '#a1dbcd', command = clear)

Now we need to place all the widgets that we have created in their respective places using the grid method. You can also use pack if you are not comfortable with grid. To know more about grid and pack visit: Tkinter pack() , grid() Method In Python

label.grid(row = 0, column = 2)
label1.grid(row = 2, column = 2)
label2.grid(row = 5, column = 2)
label3.grid(row = 7, column = 2)
label4.grid(row = 9, column = 2)

city_f.grid(row = 3, column = 2, ipadx ="180")
temp_f.grid(row = 6, column = 2, ipadx ="180")
humid_f.grid(row = 8, column = 2, ipadx ="180")
desc_f.grid(row = 10, column = 2, ipadx ="180")

b1.grid(row = 4, column = 2)
b2.grid(row = 11, column = 2)

Designing our tkinter window is done at this point.
Now we need to write the logic for extracting the weather data from OpenWeatherMap API.

Using OpenWeatherMap API to get data in Python

We have finally come to the most important part of this program. For this, we create a  function omw() which, if you recall, will be called when we click the “Tell Weather” button.

Before we start with this section, you need to get your own API key from the Open Weather Map website
You need to sign-up on the website to do this. For a more detailed explanation -> click here

We authenticate using an API key. The authenticated connection to the API is stored in the owm_obj object. You will have to provide the API key that you got after signing up at  Open Weather Map website in place of <Enter_API_KEY>

def omw() :
    api_key = "<Enter_API_KEY>"    #Enter your own API Key
    owm_obj=pyowm.OWM(api_key)

Using the get() method, we will get the name of the city entered in the city field. Further, we create an observation object through the weather_at_place() method.

city_name = city_f.get()
obs_obj=owm_obj.weather_at_place(city_name)

To get all the current weather information, we will create the weather object.

weather=obs_obj.get_weather()

We now get the values by making use of the different methods of pyowm. The library provides many methods to access weather data. It is only a matter of our requirement as to which one suits our needs the most. Here we have used the get_temperature(), get_humidity() and the get_detailed_status() i.e. description method.

temp = weather.get_temperature('celsius')["temp"]
humidity = weather.get_humidity()
description = weather.get_detailed_status()

We need to insert the values that we have obtained, to their respective text fields. And with that, your omw() function is done!

temp_f.insert(15, str(temp)+ " Celcius " )
humid_f.insert(15, str(humidity) + " %")
desc_f.insert(10, str(description) )

Just write the clear function to clear the text boxes after every run. Otherwise, the output just comes appended to the previous output.

def clear() :
    city_f.delete(0, END)
    temp_f.delete(0, END)
    humid_f.delete(0, END)
    desc_f.delete(0, END)

Now just start the GUI in the driver code and you’re all set!

root.mainloop()

Output:

Weather script using OpenWeatherMap in Python

 

Here’s the whole code:

import pyowm
from tkinter import *

def omw() :
    api_key = "<Enter_API_Key>"
    owm_obj=pyowm.OWM(api_key)

    city_name = city_f.get()
    obs_obj=owm_obj.weather_at_place(city_name)     
    weather=obs_obj.get_weather()

    temp = weather.get_temperature('celsius')["temp"]
    humidity = weather.get_humidity()
    description = weather.get_detailed_status()

    temp_f.insert(15, str(temp)+ " Celcius " )
    humid_f.insert(15, str(humidity) + " %")
    desc_f.insert(10, str(description) )

def clear() :
    city_f.delete(0, END)
    temp_f.delete(0, END)
    humid_f.delete(0, END)
    desc_f.delete(0, END)

#Driver code
root = Tk()
root.title("Weather")
root.configure(background = "#a1dbcd")
root.geometry("500x480")

label = Label(root, text = "Weather Script", fg = '#a1dbcd', bg = '#383a39')
label1 = Label(root, text = "Enter the City :", fg = 'black', bg = '#a1dbcd')
label2 = Label(root, text = "Temperature :", fg = 'black', bg = '#a1dbcd')
label3 = Label(root, text = "Humidity :",fg = 'black', bg = '#a1dbcd')
label4 = Label(root, text = "Description  :",  fg = 'black', bg = '#a1dbcd')

city_f = Entry(root)
temp_f = Entry(root)
humid_f = Entry(root)
desc_f = Entry(root)

b1 = Button(root, text = "Tell Weather!", bg = '#383a39',fg = '#a1dbcd', command = omw)
b2 = Button(root, text = "Clear", bg = '#383a39', fg = '#a1dbcd', command = clear)

label.grid(row = 0, column = 2)
label1.grid(row = 2, column = 2)
label2.grid(row = 5, column = 2)
label3.grid(row = 7, column = 2)
label4.grid(row = 9, column = 2)
city_f.grid(row = 3, column = 2, ipadx ="180")
temp_f.grid(row = 6, column = 2, ipadx ="180")
humid_f.grid(row = 8, column = 2, ipadx ="180")
desc_f.grid(row = 10, column = 2, ipadx ="180")
b1.grid(row = 4, column = 2)
b2.grid(row = 11, column = 2)

root.mainloop()

Leave a Reply

Your email address will not be published. Required fields are marked *