You are on page 1of 6

Weather App in Python | Tkinter – GUI

In this tutorial, you will learn about how to create a GUI Weather App
in Python. It uses Open Weather Map API to fetch the latest weather
information of cities and places around the globe. Also, we will be
implementing the weather app with GUI (Graphical User Interface)
rather than the traditional boring ways, which are widely available,
showing output in CLI(Command Line Interface).

Code for Weather App in Python – GUI


Without further ado, let’s get right into the code setup for creating
our GUI weather app in Python

from tkinter import *


from tkinter import ttk
import requests
2. Initialize the Tkinter Window

As a next step, we initialize our GUI window using the Tkinter module.

win = Tk()

win.title("Back Bancher Tech")


win.config(bg="blue")
win.geometry("500x570")
3. OpenWeatherMap API

In our code, we will be using Open Weather API (free tier) to get the
current weather information which is accurate and latest.

 To do so, go to OpenWeatherMap website and create an account.


 After creating your account, go to profile and then “My API keys“.
 This will open a webpage for your API Key, as shown below, copy it
for later use in code in the next step.

Open Weather Map API


4. Weather Function

Here, comes the part where we add functionality to our code. This part
is the most crucial in getting correct weather information, as this
involves fetching data from the API and displaying it in an accurate
format.

We code the most important function of this code, which is for


displaying weather, we do so as in the code:
def data_get():
city = city_name.get()
data = requests.get("https://api.openweathermap.org/data/2.5/weather?q="+city+"&appid=3c91f2ad78c
w_label1.config(text=data["weather"][0]["main"])
wb_label1.config(text=data["weather"][0]["description"])
temp_label1.config(text=str(data["main"]["temp"]-273.15))
pre_label1.config(text=data["main"]["pressure"])
to display output
As a final step to adding functionality, we add a function to change
the time format, this function checks for the local time as compared to
the UTC(Universal Time Coordinated) in which the API gives the 5.
Coding the GUI (frontend elements)
We now start to code the elements as per the GUI, for heading, text,
labels, buttons, etc.

To start with, we code the text field for the City Name we want the
weather for, along with the label to indicate so:
 We use the Label method to generate a label of text to indicate the
purpose of the input field for city name.
 Entry method is used to make an entry field for input of city name, to
check its weather.
 The textvaraible widget is used to store the inputted value, in the
variable named: city_value
 Other than these widgets we have also applied some styling to our code,
by font size, color, etc.
 name_label = Label ( win,text="Back Bancher Weather App",
 font=("Time New Roman",25,"bold"))
 name_label.place(x=25,y=50,height=50, width=450)
 city_name = StringVar()
 list_name = ["Andhra Pradesh","Arunachal Pradesh ","Assam","Bihar","Chhattisgarh","Goa","Gujarat"
Kashmir","Jharkhand","Karnataka","Kerala","Madhya
Pradesh","Maharashtra","Manipur","Meghalaya","Mizoram","Nagaland","Odisha","Punjab","Rajasthan","
Pradesh","Uttarakhand","West Bengal","Andaman and Nicobar Islands","Chandigarh","Dadra and Nagar
Capital Territory of Delhi","Puducherry"]
 com = ttk.Combobox(win,text="Back Bancher Weather App",values=list_name,
 font=("Time New Roman",15,"bold"),textvariable=city_name)
 com.place(x=25,y=120,height=50, width=450)


We code a Check Weather Button, on which we click to check the
weather of the user inputted city:
 We give our button some styling, along with the name – ‘Check
Weather’. We use the ‘command‘ widget, which shows what function
(here, showWeather function) would run on the click (key press) of
the button, as coded in the previous step.
After adding this, we add the output elements in our code. The
elements on which our Weather information would be displayed.

 Yet again, we add a label to title our result in the following text box
 To display the output we use a text field, which gets its value, every
time the “Check Weather” button is pressed. This envokes the
function to check weather info fetched from the API after processing,
[output from the showWeather function]
On execution of our code, the Tkinter displays this as output:

Weather App Frontend In Python


Final Code for GUI Weather App in Python
from tkinter import *
from tkinter import ttk
import requests

def data_get():
city = city_name.get()
data = requests.get("https://api.openweathermap.org/data/2.5/weather?q="+city+"&appid=3c91f2ad78cc
w_label1.config(text=data["weather"][0]["main"])
wb_label1.config(text=data["weather"][0]["description"])
temp_label1.config(text=str(data["main"]["temp"]-273.15))
pre_label1.config(text=data["main"]["pressure"])

win = Tk()

win.title("Back Bancher Tech")


win.config(bg="blue")
win.geometry("500x570")

name_label = Label ( win,text="Back Bancher Weather App",


font=("Time New Roman",25,"bold"))
name_label.place(x=25,y=50,height=50, width=450)
city_name = StringVar()
list_name = ["Andhra Pradesh","Arunachal Pradesh ","Assam","Bihar","Chhattisgarh","Goa","Gujarat","Har
Pradesh","Maharashtra","Manipur","Meghalaya","Mizoram","Nagaland","Odisha","Punjab","Rajasthan","Sikki
Nicobar Islands","Chandigarh","Dadra and Nagar Haveli","Daman and Diu","Lakshadweep","National Capital
com = ttk.Combobox(win,text="Back Bancher Weather App",values=list_name,
font=("Time New Roman",15,"bold"),textvariable=city_name)
com.place(x=25,y=120,height=50, width=450)

w_label = Label ( win,text="Weather Climate",


font=("Time New Roman",15))
w_label.place(x=25,y=260,height=50, width=150)

w_label1 = Label ( win,text="",


font=("Time New Roman",15))
w_label1.place(x=250,y=260,height=50, width=150)

wb_label = Label ( win,text="Weather Description",


font=("Time New Roman",13))
wb_label.place(x=25,y=330,height=50, width=150)
wb_label1 = Label ( win,text="",
font=("Time New Roman",15))
wb_label1.place(x=250,y=330,height=50, width=150)

temp_label = Label ( win,text="Temperature",


font=("Time New Roman",17))
temp_label.place(x=25,y=400,height=50, width=150)
temp_label1 = Label ( win,text="",
font=("Time New Roman",15))
temp_label1.place(x=250,y=400,height=50, width=150)

pre_label = Label ( win,text="Pressure",


font=("Time New Roman",13))
pre_label.place(x=25,y=470,height=50, width=150)
pre_label1 = Label ( win,text="",
font=("Time New Roman",15))
pre_label1.place(x=250,y=470,height=50, width=150)

done_button = Button(win,text="Done",
font=("Time New Roman",15,"bold"),command=data_get)
done_button.place(y=190,height=50,width=100,x=200)

win.mainloop()

The Output of the GUI based Weather App is shown below:

Output for Weather App

You might also like