/
show_weather.py
76 lines (53 loc) · 2.65 KB
/
show_weather.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
from fetch import fetch_data
from datetime import datetime
from sequel import *
import tkinter as tk
def get_weather(root):
create_table()
city = textField.get()
api_data = fetch_data(city)
if api_data['cod'] == '404':
print('\n```````````````````````````````````````')
print(city + ' ' + api_data['message'])
print('```````````````````````````````````````')
final_info = 'null' + '\n' + 'null'
final_data = '\nCity name: ' + 'null' + '\nCity id: ' + 'null' + '\n' + '\nHumidity: ' + 'null' + '\nWind speed: ' + 'null' + '\nVisibility: ' + 'null'
label1.config(text = final_info)
label2.config(text = final_data)
else:
weather_desc = str(api_data['weather'][0]['description'])
temp_city = str(float(api_data['main']['temp']) - 273.15)[:4] + '°C'
humid = str(api_data['main']['humidity']) + '%'
wind_spd = str(api_data['wind']['speed']) + ' kmph'
city_name = str(api_data['name'])
city_id = str(api_data['id'])
date_time = '[' + str(datetime.now().strftime("%d-%b-%Y | %I:%M %p")) + ']'
visibility = str(float(api_data['visibility'])/1000)[:4] + ' km'
insert_into_table(city_id, city_name, date_time, temp_city, weather_desc, wind_spd, humid, visibility)
print('\n\n------------------------------------------------------------------------------------')
print('Weather stats for -> {} | City-id : {} | {}'.format(city_name, city_id, date_time))
print('------------------------------------------------------------------------------------\n')
print('Current Temperature : {}'.format(temp_city))
print('Weather Discription : {}'.format(weather_desc))
print('Wind Speed : {}'.format(wind_spd))
print('Humidity : {}'.format(humid))
print('Visibility : {}\n'.format(visibility))
final_info = weather_desc + '\n' + temp_city
final_data = '\nCity name: ' + city_name + '\nCity id: ' + city_id + '\n' + date_time + '\nHumidity: ' + humid + '\nWind speed: ' + wind_spd + '\nVisibility: ' + visibility
label1.config(text = final_info)
label2.config(text = final_data)
# GUI
root = tk.Tk()
root.geometry('600x450')
root.title('Weather Forecast App')
f = ("poppins", 15, "bold" )
t = ("poppins", 35, "bold")
textField = tk.Entry(root, width = 15, font = t, bg = 'grey')
textField.pack(pady = 20)
textField.focus()
textField.bind('<Return>', get_weather)
label1 = tk.Label(root, font=t, bg = 'red2', fg = 'alice blue')
label1.pack()
label2 = tk.Label(root, font=f, bg = 'cyan4', fg = 'snow2')
label2.pack()
root.mainloop()