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