-
Notifications
You must be signed in to change notification settings - Fork 12
/
app.py
295 lines (237 loc) · 10.1 KB
/
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
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
# -*- coding: utf-8 -*-
import os
import sys
import json
import time
import random
from datetime import datetime
from module.uimap import *
from module.weather_api import OpenWeatherAPI
import requests
try :
import Tkinter as tkinter
except ImportError:
import tkinter
from tkinter.constants import *
import tkinter.messagebox
from PIL import Image, ImageTk
class App:
def __init__(self, root, title, user):
self.root = root
self.root.title('piWeatherman')
# Set Geometry to your Raspberry Pi Display Size
# In case you don't want it fullscreen
self.root.geometry('460x300') # Default is 460x300
self.mainframe = tkinter.Frame(self.root)
self.user = user
if (self.user is None):
self.registerGUI()
else :
self.weathermanGUI()
self.mainframe.pack(fill=BOTH, expand=True)
def registerGUI(self):
form = tkinter.Frame(self.mainframe)
# Color Scheme : Sort by elements
BACKGROUND = SEMI_CLOUDY_DAY
FOREGROUND = WHITE
BUTTONS = SEMI_CLOUDY_DAY_ACCENT
page_title = tkinter.Label(
form, text='Registration', font=('Roboto', 20),
background=BACKGROUND, foreground='#FFF'
).pack(pady=10)
fullname_label = tkinter.Label(
form, text='Full Name', width=20, font=('Roboto', 14),
background=BACKGROUND, foreground=WHITE
).pack()
fullname_entry = tkinter.Entry(form, bd=0, justify=CENTER)
fullname_entry.pack()
key_label = tkinter.Label(
form, text='API Key', width=20, font=('Roboto', 14),
background=BACKGROUND, foreground=WHITE
).pack()
key_entry = tkinter.Entry(form, bd=0, justify=CENTER)
key_entry.pack()
locality_label = tkinter.Label(
form, text='Locality', width=20, font=('Roboto', 14),
background=BACKGROUND, foreground=WHITE
).pack()
locality_entry = tkinter.Entry(form, bd=0, justify=CENTER)
locality_entry.pack()
def create_user():
username = fullname_entry.get()
api_key = key_entry.get()
locale = locality_entry.get()
if (confirmation.get()):
with open('data/user.json', 'w') as user_information:
self.user = {
"name": str(username),
"api-key": str(api_key),
"locale": str(locale)
}
json.dump(self.user, user_information)
form.destroy()
self.weathermanGUI()
else :
tkinter.messagebox.showinfo(
title='Check Confirmation', message='Click confirm to continue'
)
confirmation = tkinter.IntVar()
confirmation.set(0)
confirmation_check = tkinter.Checkbutton(
form, variable=confirmation, onvalue=1, offvalue=0, fg=BLACK,
text="Confirm Information?", bg=BACKGROUND, bd=0, highlightthickness=0
).pack(pady=10)
submitButton = tkinter.Button(
form, text='Submit', font=('Roboto', 14), width=20,
background=BUTTONS, foreground=WHITE, command=create_user, bd=0
).pack()
form.configure(background=BACKGROUND)
form.pack(fill=BOTH, expand=True)
def weathermanGUI(self):
window = tkinter.Frame(self.mainframe)
header_frame = tkinter.Frame(window)
middle_frame = tkinter.Frame(window)
footer_frame = tkinter.Frame(window)
def update_window():
""" Updates the window and weather data every 10 Minutes """
weather_api = OpenWeatherAPI(key=self.user['api-key'])
current_weather = weather_api.current_weather(q=self.user['locale'])
# Safe Checking : Incase of Bad Requests
if (current_weather['cod'] == 200):
weather_now = current_weather
else :
print('Error {code} : {message}'.format(
code=current_weather['cod'],
message=current_weather['message']
))
sys.exit()
# Color Scheme (sort by element) open module/uimap for changes
weather_id = weather_now['weather'][0]['id']
hour_now = time.strftime('%H')
# This maps the weather from uimap.py and https://openweathermap.org/weather-conditions
for id, name in weather_code_range:
if (weather_id in id):
key_descriptor = name
break
# Find a better method to map this
FOREGROUND = ui_map['foreground']
if int(hour_now) >= 6 and int(hour_now) <= 18:
# DAY TIME
BACKGROUND = ui_map[key_descriptor]['day']['background']
ACCENT = ui_map[key_descriptor]['day']['accent']
IMAGE = ui_map[key_descriptor]['day']['image']
else :
# NIGHT TIME
BACKGROUND = ui_map[key_descriptor]['night']['background']
ACCENT = ui_map[key_descriptor]['night']['accent']
IMAGE = ui_map[key_descriptor]['night']['image']
weather_title.configure(text='{weather} at\n{locale}'.format(
weather=(weather_now['weather'][0]['description']).capitalize(),
locale=(self.user['locale']).capitalize()
), background=BACKGROUND)
forecast_img = ImageTk.PhotoImage(Image.open(IMAGE))
forecast_label.configure(image=forecast_img, background=BACKGROUND)
forecast_label.image = forecast_img
env_detail_label.configure(background=ACCENT, foreground=FOREGROUND)
# Change this if you want to switch (default : Celcius)
k_to_c = int(weather_now['main']['temp'] - 273)
k_to_f = (weather_now['main']['temp'] - 32) * (5/9)
temp_giant_label.configure(
text='{temp}°C'.format(temp=k_to_c), background=WHITE, foreground=ACCENT)
wind_label.configure(
text='Wind Speed : {wind} m/s'.format(wind=weather_now['wind']['speed']),
background=WHITE, foreground=ACCENT)
humid_label.configure(
text='Humidity : {humid}%'.format(humid=weather_now['main']['humidity']),
background=WHITE, foreground=ACCENT)
pressure_label.configure(
text='Pressure : {press} hPa'.format(press=weather_now['main']['pressure']),
background=WHITE, foreground=ACCENT)
header_frame.configure(background=BACKGROUND)
window.configure(background=BACKGROUND)
# Set to update every 10 Minutes (1000 ms = 1 second)
# NOTE: Don't set the interval to close, weather doesn't change to often
window.after(600000, update_window)
# Header Forecast and City
weather_title = tkinter.Label(
header_frame, width=20, font=('Roboto', 18), foreground=WHITE, justify=LEFT
)
weather_title.pack(side=LEFT)
forecast_label = tkinter.Label(header_frame)
forecast_label.pack(side=RIGHT, padx=20)
# Middle Frame (Temperature and Details)
env_detail_label = tkinter.Label(
middle_frame, text='Environment Details', width=20, font=('Roboto', 14),
justify=CENTER
)
env_detail_label.pack(fill=X)
env_detail = tkinter.Frame(middle_frame)
env_detail.pack(fill=X, side=RIGHT, anchor=E, pady=20, padx=25)
temp_frame = tkinter.Frame(middle_frame)
temp_frame.pack(fill=X, side=LEFT, anchor=W, pady=20)
temp_giant_label = tkinter.Label(
temp_frame, width=10, font=('Roboto', 40)
)
temp_giant_label.pack(side=LEFT, anchor=E)
wind_label = tkinter.Label(
env_detail, font=('Roboto', 12)
)
wind_label.pack(fill=X, anchor=W)
humid_label = tkinter.Label(
env_detail, font=('Roboto', 12)
)
humid_label.pack(fill=X, anchor=W)
pressure_label = tkinter.Label(
env_detail, font=('Roboto', 12), justify=LEFT
)
pressure_label.pack(fill=X, anchor=W)
# Footer Frame (Teleprompter)
clock_label = tkinter.Label(
footer_frame, width=20, font=('Roboto', 12),
background=BLACK, foreground=WHITE, justify=LEFT
)
clock_label.pack(fill=BOTH)
def update_time():
""" Update time every second """
current_time = time.strftime('%H:%M:%S')
clock_label.configure(text=current_time)
footer_frame.after(1000, update_time)
update_time()
header_frame.pack(fill=BOTH, anchor=N, expand=True)
middle_frame.pack(fill=BOTH, anchor=CENTER, expand=True)
footer_frame.pack(fill=BOTH, anchor=S, expand=True)
middle_frame.configure(background=WHITE)
footer_frame.configure(background=BLACK)
window.pack(fill=BOTH, expand=True)
update_window()
if __name__ == '__main__':
root = tkinter.Tk()
title = 'piWeatherman'
if (os.path.exists('data/user.json')):
with open('data/user.json', 'r') as user_information:
user = json.load(user_information)
else :
user = None
# By default, fullscreen is Off
fullscreen = False
def fullscreenToggle():
global fullscreen
if (fullscreen):
fullscreen = False
else :
fullscreen = True
return fullscreen
app = App(root, title, user)
icon = ImageTk.PhotoImage(Image.open('assets/image/icon/icon.ico'))
# Compatible with python 2 and python 3
try :
root.iconphoto(True, icon)
except AttributeError:
root.tk.call('wm', 'iconphoto', root._w, icon)
try :
root.bind("<F11>", lambda event : root.attributes('-fullscreen', fullscreenToggle()))
root.bind("<Escape>", lambda event:root.destroy())
root.mainloop()
except(EOFError, KeyboardInterrupt) :
root.destroy()
sys.exit()