Added metric units option

This commit is contained in:
PySimpleGUI 2022-03-07 16:06:31 -05:00
parent 186b16e77a
commit 64f7615a25
1 changed files with 18 additions and 4 deletions

View File

@ -78,6 +78,7 @@ def change_settings(settings, window_location=(None, None)):
[sg.I(settings.get('-location-', nearest_postal), size=(15, 1), key='-LOCATION-')],
[sg.I(settings.get('-country-', 'US'), size=(15, 1), key='-COUNTRY-')],
[sg.I(settings.get('-api key-', ''), size=(32, 1), key='-API KEY-')],
[sg.CBox('Use Metric For Temperatures', default=settings.get('-celsius-', False),key='-CELSIUS-')],
[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)], ]
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)):
user_location = settings['-location-'] = values['-LOCATION-']
settings['-country-'] = values['-COUNTRY-']
API_KEY = settings['-api key-'] = values['-API KEY-']
settings['-celsius-'] = values['-CELSIUS-']
else:
API_KEY = settings['-api key-']
user_location = settings['-location-']
@ -106,6 +108,10 @@ def change_settings(settings, window_location=(None, None)):
APP_DATA['City'] = user_location
APP_DATA['Postal'] = ''
APP_DATA['Country'] = settings['-country-']
if settings['-celsius-']:
APP_DATA['Units'] = 'metric'
else:
APP_DATA['Units'] = 'imperial'
return settings
@ -152,16 +158,19 @@ def request_weather_data(endpoint):
'Is your API Key set correctly?',
API_KEY, keep_on_top=True, location=win_location)
return
if APP_DATA['Units'] == 'metric':
temp_units, speed_units = '°C', 'm/sec'
else:
temp_units, speed_units = '°F', 'miles/hr'
if response.reason == 'OK':
weather = json.loads(response.read())
APP_DATA['City'] = weather['name'].title()
APP_DATA['Description'] = weather['weather'][0]['description']
APP_DATA['Temp'] = "{:,.0f}°F".format(weather['main']['temp'])
APP_DATA['Temp'] = "{:,.0f}{}".format(weather['main']['temp'], temp_units)
APP_DATA['Humidity'] = "{:,d}%".format(weather['main']['humidity'])
APP_DATA['Pressure'] = "{:,d} hPa".format(weather['main']['pressure'])
APP_DATA['Feels Like'] = "{:,.0f}°F".format(weather['main']['feels_like'])
APP_DATA['Wind'] = "{:,.1f} m/h".format(weather['wind']['speed'])
APP_DATA['Feels Like'] = "{:,.0f}{}".format(weather['main']['feels_like'], temp_units)
APP_DATA['Wind'] = "{:,.1f}{}".format(weather['wind']['speed'], speed_units)
APP_DATA['Precip 1hr'] = None if not weather.get('rain') else "{:2} mm".format(weather['rain']['1h'])
APP_DATA['Updated'] = 'Updated: ' + datetime.datetime.now().strftime("%B %d %I:%M:%S %p")
APP_DATA['Lon'] = weather['coord']['lon']
@ -251,6 +260,11 @@ def main(refresh_rate, win_location):
settings = load_settings()
location = settings['-location-']
APP_DATA['Country'] = settings.get('-country-', 'US')
if settings.get('-celsius-'):
APP_DATA['Units'] = 'metric'
else:
APP_DATA['Units'] = 'imperial'
if location is not None:
if location.isnumeric() and len(location) == 5 and location is not None:
APP_DATA['Postal'] = location