Open In App

Weather App in Python using Tkinter module

In this article, we are going to discuss how to create a weather app using tkinter. The GUI app will tell us the current weather of a particular city along with temperature details along with other details. 

Modules required:

pip install requests

Approach:

Firstly, we have to use a weather API for fetching the data from the Open Weather Map website by generating an API key, and then we need to create a configuration file to store the key. And finally using that configuration file in the python script.



Steps to generate an API key:

Steps to create the Configuration file:



Steps to create the Python script:




# import required modules
from configparser import ConfigParser
import requests
from tkinter import *
from tkinter import messagebox




# create object
app = Tk()
  
# add title
app.title("Weather App")
  
# adjust window size
app.geometry("300x300")
  
# add labels, buttons and text
city_text = StringVar()
city_entry = Entry(app, textvariable=city_text)
city_entry.pack()
  
Search_btn = Button(app, text="Search Weather",
                    width=12, command=search)
Search_btn.pack()
  
location_lbl = Label(app, text="Location"
                     font={'bold', 20})
location_lbl.pack()
  
temperature_label = Label(app, text="")
temperature_label.pack()
  
weather_l = Label(app, text="")
weather_l.pack()
  
app.mainloop()




# extract key from the 
# configuration file
config_file = "config.ini"
config = ConfigParser()
config.read(config_file)
api_key = config['gfg']['api']




# explicit function to get
# weather details
def getweather(city):
    result = requests.get(url.format(city, api_key))
      
    if result:
        json = result.json()
        city = json['name']
        country = json['sys']
        temp_kelvin = json['main']['temp']
        temp_celsius = temp_kelvin-273.15
        weather1 = json['weather'][0]['main']
        final = [city, country, temp_kelvin, 
                 temp_celsius, weather1]
        return final
    else:
        print("NO Content Found")




# explicit function to
# search city
def search():
    city = city_text.get()
    weather = getweather(city)
      
    if weather:
        location_lbl['text'] = '{} ,{}'.format(weather[0], weather[1])
        temperature_label['text'] = str(weather[3])+"   Degree Celsius"
        weather_l['text'] = weather[4]
          
    else:
        messagebox.showerror('Error', "Cannot find {}".format(city))

Below is the complete program:




# import required modules
from configparser import ConfigParser
import requests
from tkinter import *
from tkinter import messagebox
  
# extract key from the
# configuration file
config_file = "config.ini"
config = ConfigParser()
config.read(config_file)
api_key = config['gfg']['api']
  
  
# explicit function to get
# weather details
def getweather(city):
    result = requests.get(url.format(city, api_key))
      
    if result:
        json = result.json()
        city = json['name']
        country = json['sys']
        temp_kelvin = json['main']['temp']
        temp_celsius = temp_kelvin-273.15
        weather1 = json['weather'][0]['main']
        final = [city, country, temp_kelvin, 
                 temp_celsius, weather1]
        return final
    else:
        print("NO Content Found")
  
  
# explicit function to
# search city
def search():
    city = city_text.get()
    weather = getweather(city)
    if weather:
        location_lbl['text'] = '{} ,{}'.format(weather[0], weather[1])
        temperature_label['text'] = str(weather[3])+"   Degree Celsius"
        weather_l['text'] = weather[4]
    else:
        messagebox.showerror('Error', "Cannot find {}".format(city))
  
  
# Driver Code
# create object
app = Tk()
# add title
app.title("Weather App")
# adjust window size
app.geometry("300x300")
  
# add labels, buttons and text
city_text = StringVar()
city_entry = Entry(app, textvariable=city_text)
city_entry.pack()
Search_btn = Button(app, text="Search Weather"
                    width=12, command=search)
Search_btn.pack()
location_lbl = Label(app, text="Location", font={'bold', 20})
location_lbl.pack()
temperature_label = Label(app, text="")
temperature_label.pack()
weather_l = Label(app, text="")
weather_l.pack()
app.mainloop()

Output:


Article Tags :