Skip to content

Commit 64f7615

Browse files
committed
Added metric units option
1 parent 186b16e commit 64f7615

File tree

1 file changed

+18
-4
lines changed

1 file changed

+18
-4
lines changed

DemoPrograms/Demo_Desktop_Widget_Weather.py

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ def change_settings(settings, window_location=(None, None)):
7878
[sg.I(settings.get('-location-', nearest_postal), size=(15, 1), key='-LOCATION-')],
7979
[sg.I(settings.get('-country-', 'US'), size=(15, 1), key='-COUNTRY-')],
8080
[sg.I(settings.get('-api key-', ''), size=(32, 1), key='-API KEY-')],
81+
[sg.CBox('Use Metric For Temperatures', default=settings.get('-celsius-', False),key='-CELSIUS-')],
8182
[sg.B('Ok', border_width=0, bind_return_key=True), sg.B('Register For a Key', border_width=0, k='-REGISTER-'), sg.B('Cancel', border_width=0)], ]
8283

8384
window = sg.Window('Settings', layout, location=window_location, no_titlebar=True, keep_on_top=True, border_depth=0)
@@ -94,6 +95,7 @@ def change_settings(settings, window_location=(None, None)):
9495
user_location = settings['-location-'] = values['-LOCATION-']
9596
settings['-country-'] = values['-COUNTRY-']
9697
API_KEY = settings['-api key-'] = values['-API KEY-']
98+
settings['-celsius-'] = values['-CELSIUS-']
9799
else:
98100
API_KEY = settings['-api key-']
99101
user_location = settings['-location-']
@@ -106,6 +108,10 @@ def change_settings(settings, window_location=(None, None)):
106108
APP_DATA['City'] = user_location
107109
APP_DATA['Postal'] = ''
108110
APP_DATA['Country'] = settings['-country-']
111+
if settings['-celsius-']:
112+
APP_DATA['Units'] = 'metric'
113+
else:
114+
APP_DATA['Units'] = 'imperial'
109115

110116
return settings
111117

@@ -152,16 +158,19 @@ def request_weather_data(endpoint):
152158
'Is your API Key set correctly?',
153159
API_KEY, keep_on_top=True, location=win_location)
154160
return
155-
161+
if APP_DATA['Units'] == 'metric':
162+
temp_units, speed_units = '°C', 'm/sec'
163+
else:
164+
temp_units, speed_units = '°F', 'miles/hr'
156165
if response.reason == 'OK':
157166
weather = json.loads(response.read())
158167
APP_DATA['City'] = weather['name'].title()
159168
APP_DATA['Description'] = weather['weather'][0]['description']
160-
APP_DATA['Temp'] = "{:,.0f}°F".format(weather['main']['temp'])
169+
APP_DATA['Temp'] = "{:,.0f}{}".format(weather['main']['temp'], temp_units)
161170
APP_DATA['Humidity'] = "{:,d}%".format(weather['main']['humidity'])
162171
APP_DATA['Pressure'] = "{:,d} hPa".format(weather['main']['pressure'])
163-
APP_DATA['Feels Like'] = "{:,.0f}°F".format(weather['main']['feels_like'])
164-
APP_DATA['Wind'] = "{:,.1f} m/h".format(weather['wind']['speed'])
172+
APP_DATA['Feels Like'] = "{:,.0f}{}".format(weather['main']['feels_like'], temp_units)
173+
APP_DATA['Wind'] = "{:,.1f}{}".format(weather['wind']['speed'], speed_units)
165174
APP_DATA['Precip 1hr'] = None if not weather.get('rain') else "{:2} mm".format(weather['rain']['1h'])
166175
APP_DATA['Updated'] = 'Updated: ' + datetime.datetime.now().strftime("%B %d %I:%M:%S %p")
167176
APP_DATA['Lon'] = weather['coord']['lon']
@@ -251,6 +260,11 @@ def main(refresh_rate, win_location):
251260
settings = load_settings()
252261
location = settings['-location-']
253262
APP_DATA['Country'] = settings.get('-country-', 'US')
263+
if settings.get('-celsius-'):
264+
APP_DATA['Units'] = 'metric'
265+
else:
266+
APP_DATA['Units'] = 'imperial'
267+
254268
if location is not None:
255269
if location.isnumeric() and len(location) == 5 and location is not None:
256270
APP_DATA['Postal'] = location

0 commit comments

Comments
 (0)