- Get link
- X
- Other Apps
- Get link
- X
- Other Apps
from tkinter import *
from tkinter import messagebox
import requests
from configparser import ConfigParser
#now get the config.ini file
config_file="C:\\Users\\Fahad Ali Khan\\Desktop\\config.ini"
config=ConfigParser()
config.read(config_file)
api_key=config['gfg']['api']
url="http://api.openweathermap.org/data/2.5/weather?q={}&appid={}"
#create a function to get weather of specificlocation
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_celcious=temp_kelvin-273.15
weather1=json['weather'][0]['main']
final=[city,country,temp_kelvin,temp_celcious,weather1]
return final
else:
print("no content found")
def search():
city = e1.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))
#GUI Section
root=Tk()
root.geometry("700x400")
root.title("Weather App")
e1=StringVar()
ent=Entry(root,textvariable=e1)
ent.pack()
bt=Button(root,text="show weather",command=search)
bt.pack()
location_lbl = Label(root, text="Location", font={'bold', 20})
location_lbl.pack()
temperature_label = Label(root, text="")
temperature_label.pack()
weather_l = Label(root, text="")
weather_l.pack()
root.mainloop()
- Get link
- X
- Other Apps
Comments
Post a Comment