-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
108 lines (87 loc) · 2.96 KB
/
main.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
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk
from fetch_data import get_24h_forecast_temps
from plotting import draw_chart_forcast
import cache_helper as ch
from datetime import datetime
import json
class Handler(Gtk.Window):
def onDestroy(self, *args):
Gtk.main_quit()
def on_btn_fetch_clicked(self, widget):
city_id = combo_box.get_active_id()
cached_forecasts = ch.get(city_id)
forecasts = get_24h_forecast_temps(city_id)
if cached_forecasts:
cached_forecasts = {**cached_forecasts, **forecasts}
ch.set(city_id, cached_forecasts)
i = 0
for k, v in forecasts.items():
time = datetime.fromtimestamp(k)
degrees_dict[i].set_text(str(v['main']['cel_temp']) + ' C')
time_dict[i].set_text(time.strftime('%H:%M\n%A'))
icon_dict[i].set_from_file('icons/small/%s.png' % v['weather'][0]['icon'])
if i == 0:
icon_dict[i].set_from_file('icons/large/%s.png' % v['weather'][0]['icon'])
i += 1
if i > 8:
break
def draw_chart_clicked(self, widget):
city_id = combo_box.get_active_id()
cached_forecasts = ch.get(city_id)
if not cached_forecasts:
return None
val_list = [forecast['main']['cel_temp'] for forecast in cached_forecasts.values()]
time_list = [forecast['dt_txt'] for forecast in cached_forecasts.values()]
draw_chart_forcast(time_list, val_list)
def onShow(self, widget):
pass
# with open('city.list.json') as f:
# cities = json.load(f)
builder = Gtk.Builder()
builder.add_from_file("home.glade")
builder.connect_signals(Handler())
# get element of ui designed
window = builder.get_object("win1")
# degree labels
degrees_dict = {
0: builder.get_object("degree0"),
1: builder.get_object("degree1"),
2: builder.get_object("degree2"),
3: builder.get_object("degree3"),
4: builder.get_object("degree4"),
5: builder.get_object("degree5"),
6: builder.get_object("degree6"),
7: builder.get_object("degree7"),
8: builder.get_object("degree8"),
}
# icons
icon_dict = {
0: builder.get_object("icon0"),
1: builder.get_object("icon1"),
2: builder.get_object("icon2"),
3: builder.get_object("icon3"),
4: builder.get_object("icon4"),
5: builder.get_object("icon5"),
6: builder.get_object("icon6"),
7: builder.get_object("icon7"),
8: builder.get_object("icon8"),
}
# time
time_dict = {
0: builder.get_object("time0"),
1: builder.get_object("time1"),
2: builder.get_object("time2"),
3: builder.get_object("time3"),
4: builder.get_object("time4"),
5: builder.get_object("time5"),
6: builder.get_object("time6"),
7: builder.get_object("time7"),
8: builder.get_object("time8"),
}
# combo
combo_box = builder.get_object("combo_city")
firstbox = builder.get_object("firstbox")
window.show_all()
Gtk.main()