Welcome folks today in this post we will be building weather application
using open weather map api in tkinter. All the source code of application is given below.
Get Started
In order to get started we need to install python inside our system. And now we need to install tkinter
library by using the pip command
pip install tkinter
And now we need to make an app.py
file inside the root directory and copy paste the below code
And now you need to have a open weather map api
and get an api key like this as shown below.
And now copy paste the api key inside this below code inside the file app.py
app.py
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 |
from tkinter import * import tkinter as tk from datetime import datetime from PIL import ImageTk , Image import requests from tkinter import messagebox class Weather(): def weather_report(self): self.url = "http://api.openweathermap.org/data/2.5/weather?q=" self.cityname = self.loc.get(1.0,END) self.api_key = '64f61566457c1a48b4f301389ba59786' self.data = requests.get(self.url+self.cityname+'&appid='+self.api_key).json() if self.data['cod'] == '404': messagebox.showerror('Error','City Not Found !!') else: self.location['text'] = self.data['name'] + "," + self.data['sys']['country'] self.c = self.data['main']['temp_max'] - 273.15 self.f = self.c*9/5+32 self.weather['text'] = self.data['weather'][0]['main'] self.weather['font'] = ('verdana',20,'bold') self.temperature['text'] = f'{self.c}°C \n {self.f}°F' self.temperature['font'] = ('verdana',15,'bold') self.humidity['text'] = self.data['main']['humidity'] self.humidity['font'] = ('verdana',15,'bold') self.pressure['text'] = self.data['main']['pressure'] self.pressure['font'] = ('verdana',15,'bold') def __init__(self): self.root = tk.Tk() self.root.geometry('500x300') self.root.title("Weather Application") self.root.maxsize(500,300) self.root.minsize(500,300) self.header = Label(self.root,width=100,height=2,bg="#00274c") self.header.place(x=0,y=0) self.font = ('verdana',10,'bold') self.date = Label(self.root,text=datetime.now().date(),bg="#00274c",fg="white",font=self.font) self.date.place(x=400,y=5) self.heading = Label(self.root,text="Weather Report",bg="#00274c",fg="white",font=self.font) self.heading.place(x=180,y=5) self.location = Label(self.root,text="NA-/",bg="#00274c",fg="white",font=self.font) self.location.place(x=10,y=5) self.img = ImageTk.PhotoImage(Image.open('icon.png')) self.image = Label(self.root,image=self.img) self.image.place(x=20,y=40) self.name = Label(self.root,text="City or Country Name",fg="#00274c",font=self.font) self.name.place(x=140,y=45) self.loc = Text(self.root,width=25,height=2) self.loc.place(x=140,y=70) self.button = Button(self.root,text="Search",bg="#00274c",fg="white",font=self.font,relief=RAISED,borderwidth=3,command=self.weather_report) self.button.place(x=350,y=73) self.line1 = Label(self.root,bg="#00274c",width=20,height=0) self.line1.place(x=0,y=150) self.line2 = Label(self.root,bg="#00274c",width=20,height=0) self.line2.place(x=360,y=150) self.report = Label(self.root,text="Weather Report",bg="#00274c",fg="white",font=self.font,padx=10) self.report.place(x=180,y=150) self.img2 = ImageTk.PhotoImage(Image.open('icon2.png')) self.image2 = Label(self.root,image=self.img2) self.image2.place(x=90,y=180) self.weather = Label(self.root,text="NA/-",fg="#00274c",font=self.font) self.weather.place(x=90,y=230) self.img3 = ImageTk.PhotoImage(Image.open('icon3.png')) self.image3 = Label(self.root,image=self.img3) self.image3.place(x=200,y=180) self.temperature = Label(self.root,text="NA/-",fg="#00274c",font=self.font) self.temperature.place(x=200,y=230) self.img4 = ImageTk.PhotoImage(Image.open('icon4.png')) self.image4 = Label(self.root,image=self.img4) self.image4.place(x=310,y=180) self.humidity = Label(self.root,text="NA/-",fg="#00274c",font=self.font) self.humidity.place(x=310,y=230) self.img5 = ImageTk.PhotoImage(Image.open('icon5.png')) self.image5 = Label(self.root,image=self.img5) self.image5.place(x=380,y=180) self.pressure = Label(self.root,text="NA/-",fg="#00274c",font=self.font) self.pressure.place(x=380,y=230) self.root.mainloop() if __name__ == '__main__': Weather() |
And now if you execute the application by executing the below command
python app.py