-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathweather.py
More file actions
344 lines (329 loc) · 15.6 KB
/
weather.py
File metadata and controls
344 lines (329 loc) · 15.6 KB
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'weather.ui'
#
# Created by: PyQt5 UI code generator 5.15.4
#
# WARNING: Any manual changes made to this file will be lost when pyuic5 is
# run again. Do not edit this file unless you know what you are doing.
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtGui import QPixmap
from PyQt5.QtWidgets import *
import requests
from timezonefinder import TimezoneFinder
from pytz import timezone
from datetime import datetime
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(391, 625)
icon = QtGui.QIcon()
icon.addPixmap(QtGui.QPixmap("icons8-weather-100.png"),
QtGui.QIcon.Normal, QtGui.QIcon.Off)
MainWindow.setWindowIcon(icon)
MainWindow.setStyleSheet("background-color: rgb(225, 234, 228);\n"
"")
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.frame = QtWidgets.QFrame(self.centralwidget)
self.frame.setGeometry(QtCore.QRect(10, 10, 371, 151))
font = QtGui.QFont()
font.setPointSize(10)
self.frame.setFont(font)
self.frame.setStyleSheet("background-color: rgb(148, 180, 159);")
self.frame.setFrameShape(QtWidgets.QFrame.StyledPanel)
self.frame.setFrameShadow(QtWidgets.QFrame.Raised)
self.frame.setObjectName("frame")
self.le_search = QtWidgets.QLineEdit(self.frame)
self.le_search.setGeometry(QtCore.QRect(20, 31, 311, 31))
font = QtGui.QFont()
font.setPointSize(12)
self.le_search.setFont(font)
self.le_search.setStyleSheet("background-color: rgb(238, 238, 238);\n"
"border-radius: 15px;")
self.le_search.setFrame(True)
self.le_search.setEchoMode(QtWidgets.QLineEdit.Normal)
self.le_search.setReadOnly(False)
self.le_search.setObjectName("le_search")
self.pb_search = QtWidgets.QPushButton(self.frame)
self.pb_search.setGeometry(QtCore.QRect(267, 35, 61, 25))
self.pb_search.setCursor(QtGui.QCursor(QtCore.Qt.PointingHandCursor))
self.pb_search.clicked.connect(self.search)
self.pb_search.setStyleSheet("border-radius: 10px;\n"
"background-color: rgb(238, 238, 238);")
self.pb_search.setText("")
icon1 = QtGui.QIcon()
icon1.addPixmap(QtGui.QPixmap("icons8-search-50.png"),
QtGui.QIcon.Normal, QtGui.QIcon.Off)
self.pb_search.setIcon(icon1)
self.pb_search.setIconSize(QtCore.QSize(25, 25))
self.pb_search.setObjectName("pb_search")
self.lbl_city = QtWidgets.QLabel(self.frame)
self.lbl_city.setGeometry(QtCore.QRect(31, 70, 291, 31))
font = QtGui.QFont()
font.setPointSize(14)
self.lbl_city.setFont(font)
self.lbl_city.setStyleSheet("color: rgb(51, 51, 51);")
self.lbl_city.setObjectName("lbl_city")
self.lbl_location = QtWidgets.QLabel(self.frame)
self.lbl_location.setGeometry(QtCore.QRect(22, 0, 81, 31))
font = QtGui.QFont()
font.setPointSize(12)
self.lbl_location.setFont(font)
self.lbl_location.setStyleSheet("color: rgb(51, 51, 51);")
self.lbl_location.setObjectName("lbl_location")
self.lbl_day = QtWidgets.QLabel(self.frame)
self.lbl_day.setGeometry(QtCore.QRect(30, 100, 71, 21))
font = QtGui.QFont()
font.setPointSize(10)
self.lbl_day.setFont(font)
self.lbl_day.setStyleSheet("color: rgb(51, 51, 51);")
self.lbl_day.setObjectName("lbl_day")
self.lbl_date = QtWidgets.QLabel(self.frame)
self.lbl_date.setGeometry(QtCore.QRect(110, 100, 77, 21))
font = QtGui.QFont()
font.setPointSize(10)
self.lbl_date.setFont(font)
self.lbl_date.setStyleSheet("color: rgb(51, 51, 51);")
self.lbl_date.setObjectName("lbl_date")
self.lbl_time = QtWidgets.QLabel(self.frame)
self.lbl_time.setGeometry(QtCore.QRect(30, 120, 41, 31))
font = QtGui.QFont()
font.setPointSize(10)
self.lbl_time.setFont(font)
self.lbl_time.setStyleSheet("color: rgb(51, 51, 51);")
self.lbl_time.setObjectName("lbl_time")
self.lbl_weather = QtWidgets.QLabel(self.centralwidget)
self.lbl_weather.setGeometry(QtCore.QRect(50, 190, 121, 111))
font = QtGui.QFont()
font.setPointSize(8)
self.lbl_weather.setFont(font)
self.lbl_weather.setStyleSheet("")
self.lbl_weather.setText("")
self.lbl_weather.setObjectName("lbl_")
self.lbl_temp = QtWidgets.QLabel(self.centralwidget)
self.lbl_temp.setGeometry(QtCore.QRect(200, 240, 111, 71))
font = QtGui.QFont()
font.setFamily("Dubai Light")
font.setPointSize(28)
font.setBold(False)
font.setItalic(False)
font.setWeight(50)
self.lbl_temp.setFont(font)
self.lbl_temp.setStyleSheet("font:28pt \"Dubai Light\";\n"
"color: rgb(34, 34, 34);\n"
"")
self.lbl_temp.setObjectName("lbl_temp")
self.lbl_description = QtWidgets.QLabel(self.centralwidget)
self.lbl_description.setGeometry(QtCore.QRect(165, 200, 218, 31))
font = QtGui.QFont()
font.setPointSize(11)
font.setBold(False)
font.setItalic(False)
font.setWeight(50)
font.setKerning(True)
self.lbl_description.setFont(font)
self.lbl_description.setStyleSheet("color: rgb(34, 34, 34);\n"
"")
self.lbl_description.setObjectName("lbl_description")
self.lbl_h3 = QtWidgets.QLabel(self.centralwidget)
self.lbl_h3.setGeometry(QtCore.QRect(296, 336, 75, 25))
font = QtGui.QFont()
font.setPointSize(11)
self.lbl_h3.setFont(font)
self.lbl_h3.setStyleSheet("color: rgb(34, 34, 34);")
self.lbl_h3.setText("")
self.lbl_h3.setObjectName("lbl_h3")
self.lbl_ws3 = QtWidgets.QLabel(self.centralwidget)
self.lbl_ws3.setGeometry(QtCore.QRect(296, 399, 75, 25))
font = QtGui.QFont()
font.setPointSize(11)
self.lbl_ws3.setFont(font)
self.lbl_ws3.setStyleSheet("color: rgb(34, 34, 34);")
self.lbl_ws3.setText("")
self.lbl_ws3.setObjectName("lbl_ws3")
self.lbl_h1 = QtWidgets.QLabel(self.centralwidget)
self.lbl_h1.setGeometry(QtCore.QRect(20, 336, 100, 25))
font = QtGui.QFont()
font.setPointSize(11)
self.lbl_h1.setFont(font)
self.lbl_h1.setStyleSheet("color: rgb(34, 34, 34);")
self.lbl_h1.setObjectName("lbl_h1")
self.lbl_ws1 = QtWidgets.QLabel(self.centralwidget)
self.lbl_ws1.setGeometry(QtCore.QRect(20, 399, 100, 25))
font = QtGui.QFont()
font.setPointSize(11)
self.lbl_ws1.setFont(font)
self.lbl_ws1.setStyleSheet("color: rgb(34, 34, 34);")
self.lbl_ws1.setObjectName("lbl_ws1")
self.lbl_ap3 = QtWidgets.QLabel(self.centralwidget)
self.lbl_ap3.setGeometry(QtCore.QRect(296, 460, 75, 25))
font = QtGui.QFont()
font.setPointSize(11)
self.lbl_ap3.setFont(font)
self.lbl_ap3.setStyleSheet("color: rgb(34, 34, 34);")
self.lbl_ap3.setText("")
self.lbl_ap3.setObjectName("lbl_ap3")
self.lbl_ap1 = QtWidgets.QLabel(self.centralwidget)
self.lbl_ap1.setGeometry(QtCore.QRect(20, 460, 100, 25))
font = QtGui.QFont()
font.setPointSize(11)
self.lbl_ap1.setFont(font)
self.lbl_ap1.setStyleSheet("color: rgb(34, 34, 34);")
self.lbl_ap1.setObjectName("lbl_ap1")
self.lbl_maxt1 = QtWidgets.QLabel(self.centralwidget)
self.lbl_maxt1.setGeometry(QtCore.QRect(20, 521, 100, 25))
font = QtGui.QFont()
font.setPointSize(11)
self.lbl_maxt1.setFont(font)
self.lbl_maxt1.setStyleSheet("color: rgb(34, 34, 34);")
self.lbl_maxt1.setObjectName("lbl_maxt1")
self.lbl_maxt3 = QtWidgets.QLabel(self.centralwidget)
self.lbl_maxt3.setGeometry(QtCore.QRect(296, 521, 75, 25))
font = QtGui.QFont()
font.setPointSize(11)
self.lbl_maxt3.setFont(font)
self.lbl_maxt3.setStyleSheet("color: rgb(34, 34, 34);")
self.lbl_maxt3.setText("")
self.lbl_maxt3.setObjectName("lbl_maxt3")
self.lbl_mint3 = QtWidgets.QLabel(self.centralwidget)
self.lbl_mint3.setGeometry(QtCore.QRect(296, 578, 75, 25))
font = QtGui.QFont()
font.setPointSize(11)
self.lbl_mint3.setFont(font)
self.lbl_mint3.setStyleSheet("color: rgb(34, 34, 34);")
self.lbl_mint3.setObjectName("lbl_mint3")
self.lbl_mint1 = QtWidgets.QLabel(self.centralwidget)
self.lbl_mint1.setGeometry(QtCore.QRect(20, 578, 100, 25))
font = QtGui.QFont()
font.setPointSize(11)
self.lbl_mint1.setFont(font)
self.lbl_mint1.setStyleSheet("color: rgb(34, 34, 34);")
self.lbl_mint1.setObjectName("lbl_mint1")
self.lbl_h2 = QtWidgets.QLabel(self.centralwidget)
self.lbl_h2.setGeometry(QtCore.QRect(169, 322, 51, 51))
self.lbl_h2.setText("")
self.lbl_h2.setPixmap(QtGui.QPixmap("icons8-humidity-53.png"))
self.lbl_h2.setObjectName("lbl_h2")
self.lbl_ws2 = QtWidgets.QLabel(self.centralwidget)
self.lbl_ws2.setGeometry(QtCore.QRect(169, 388, 51, 51))
self.lbl_ws2.setText("")
self.lbl_ws2.setPixmap(QtGui.QPixmap("icons8-wind-53.png"))
self.lbl_ws2.setObjectName("lbl_ws2")
self.lbl_ap2 = QtWidgets.QLabel(self.centralwidget)
self.lbl_ap2.setGeometry(QtCore.QRect(169, 449, 47, 51))
self.lbl_ap2.setText("")
self.lbl_ap2.setPixmap(QtGui.QPixmap("icons8-pressure-48.png"))
self.lbl_ap2.setObjectName("lbl_ap2")
self.lbl_maxt2 = QtWidgets.QLabel(self.centralwidget)
self.lbl_maxt2.setGeometry(QtCore.QRect(164, 509, 51, 51))
self.lbl_maxt2.setText("")
self.lbl_maxt2.setPixmap(QtGui.QPixmap(
"icons8-high-temperature-53.png"))
self.lbl_maxt2.setObjectName("lbl_maxt2")
self.lbl_mint2 = QtWidgets.QLabel(self.centralwidget)
self.lbl_mint2.setGeometry(QtCore.QRect(164, 570, 51, 51))
self.lbl_mint2.setText("")
self.lbl_mint2.setPixmap(QtGui.QPixmap(
"icons8-low-temperature-53.png"))
self.lbl_mint2.setObjectName("lbl_mint2")
MainWindow.setCentralWidget(self.centralwidget)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "Weather"))
self.lbl_city.setText(_translate("MainWindow", " "))
self.lbl_weather.setText(_translate("MainWindow", " "))
self.lbl_location.setText(_translate("MainWindow", " Location"))
self.lbl_day.setText(_translate("MainWindow", " "))
self.lbl_date.setText(_translate("MainWindow", " "))
self.lbl_time.setText(_translate("MainWindow", " "))
self.lbl_temp.setText(_translate("MainWindow", " "))
self.lbl_description.setText(_translate("MainWindow", " "))
self.lbl_h1.setText(_translate("MainWindow", " Humidity"))
self.lbl_ws1.setText(_translate("MainWindow", " Wind Speed"))
self.lbl_ap1.setText(_translate("MainWindow", " Air Pressure"))
self.lbl_maxt1.setText(_translate("MainWindow", " Max Temp"))
self.lbl_mint3.setText(_translate("MainWindow", " "))
self.lbl_mint1.setText(_translate("MainWindow", " Min Temp"))
def search(self):
# get the city name from the search field
location = self.le_search.text()
# create an account on https://api.openweathermap.org/ and get an api key
url = 'https://api.openweathermap.org/data/2.5/weather?q={}&appid=your_api_key'
try:
# now i want to send a request
res = requests.get(url.format(location))
# i get all of data from city
type_weather = res.json()['weather'][0]['main']
# get city name and set in lbl_city
city_name = res.json()['name']
self.lbl_city.setText(city_name)
# get lat and lon
lon = res.json()['coord']['lon']
lat = res.json()['coord']['lat']
# get timezone with TimezoneFinder
tf = TimezoneFinder()
query_points = [(lon, lat)]
for lon, lat in query_points:
tz = tf.timezone_at(lng=lon, lat=lat)
# get current time from timezone
current_time = datetime.now(timezone(tz))
# get time and set in lbl_time
time = current_time.strftime("%H:%M")
self.lbl_time.setText(time)
# get date and set in lbl_date
date = current_time.strftime("%Y-%m-%d")
self.lbl_date.setText(date)
# get week day and set in lbl_day
self.lbl_day.setText(current_time.strftime("%A"))
self.le_search.setText('')
# we have 7 main weather on the https://api.openweathermap.org/
if type_weather == 'Clear':
self.lbl_weather.setPixmap(QPixmap("icons8-air-100.png"))
elif type_weather == 'Clouds':
self.lbl_weather.setPixmap(QPixmap("icons8-clouds-100.png"))
elif type_weather == 'Snow':
self.lbl_weather.setPixmap(QPixmap("icons8-snow-fall-100.png"))
elif type_weather == 'Rain':
self.lbl_weather.setPixmap(QPixmap("icons8-rainfall-100.png"))
elif type_weather == 'Drizzle':
self.lbl_weather.setPixmap(QPixmap("icons8-drizzle-100.png"))
elif type_weather == 'Thunderstorm':
self.lbl_weather.setPixmap(QPixmap("icons8-thunderstorm-100.png"))
else:
self.lbl_weather.setPixmap(QPixmap("icons8-foggy-96.png"))
# get and set weather description in lbl_description
self.lbl_description.setText(
res.json()['weather'][0]['description'])
# get and set temp in lbl_temp
self.lbl_temp.setText(
str(int(res.json()['main']['temp']-273.15))+'°c')
# get and set max temp in lbl_maxt3
self.lbl_maxt3.setText(
str(int(res.json()['main']['temp_max']-273.15))+' °c')
# get and set min temp in lbl_mint3
self.lbl_mint3.setText(
str(int(res.json()['main']['temp_min']-273.15))+' °c')
# get and set humidity in lbl_h3
self.lbl_h3.setText(str(res.json()['main']['humidity'])+' %')
# get and set pressure in lbl_ap3
self.lbl_ap3.setText(str(res.json()['main']['pressure'])+' hpa')
# get and set wind speed in lbl_ws3
self.lbl_ws3.setText(
str(int(res.json()['wind']['speed']))+' k/s')
except:
msg = QMessageBox()
msg.setWindowTitle("Warning")
msg.setIcon(QMessageBox.Warning)
msg.setText("The name of the city is wrong")
msg.setStandardButtons(QMessageBox.Ok | QMessageBox.Cancel)
msg.exec_()
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())