(settings, window_location=(None, None))
| 68 | |
| 69 | |
| 70 | def change_settings(settings, window_location=(None, None)): |
| 71 | global APP_DATA, API_KEY |
| 72 | |
| 73 | try: |
| 74 | nearest_postal = json.loads(request.urlopen('http://ipapi.co/json').read())['postal'] |
| 75 | except Exception as e: |
| 76 | print('Error getting nearest postal', e) |
| 77 | nearest_postal = '' |
| 78 | |
| 79 | layout = [[sg.T('Enter Zipcode or City for your location')], |
| 80 | [sg.I(settings.get('-location-', nearest_postal), size=(15, 1), key='-LOCATION-'), sg.T('City')], |
| 81 | [sg.I(settings.get('-country-', 'US'), size=(15, 1), key='-COUNTRY-'), sg.T('Country')], |
| 82 | [sg.I(settings.get('-friends name-', ''), size=(15, 1), key='-FRIENDS NAME-'), sg.T('Who')], |
| 83 | [sg.I(settings.get('-api key-', ''), size=(32, 1), key='-API KEY-')], |
| 84 | [sg.CBox('Use Metric For Temperatures', default=settings.get('-celsius-', False),key='-CELSIUS-')], |
| 85 | [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)], ] |
| 86 | |
| 87 | window = sg.Window('Settings', layout, location=window_location, no_titlebar=True, keep_on_top=True, border_depth=0) |
| 88 | event, values = window.read() |
| 89 | window.close() |
| 90 | |
| 91 | if event == '-REGISTER-': |
| 92 | sg.popup('Launching browser so you can signup for the "Current Weather" service from OpenWeatherMap.org to get a Free API Key', 'Click OK and your browser will open', r'Visit https://home.openweathermap.org/ for more information', location=window_location) |
| 93 | # Register to get a free key |
| 94 | webbrowser.open(r'https://home.openweathermap.org/users/sign_up') |
| 95 | |
| 96 | |
| 97 | if event == 'Ok': |
| 98 | user_location = settings['-location-'] = values['-LOCATION-'] |
| 99 | settings['-country-'] = values['-COUNTRY-'] |
| 100 | API_KEY = settings['-api key-'] = values['-API KEY-'] |
| 101 | settings['-celsius-'] = values['-CELSIUS-'] |
| 102 | settings['-friends name-'] = values['-FRIENDS NAME-'] |
| 103 | else: |
| 104 | API_KEY = settings['-api key-'] |
| 105 | user_location = settings['-location-'] |
| 106 | |
| 107 | if user_location is not None: |
| 108 | if user_location.isnumeric() and len(user_location) == 5 and user_location is not None: |
| 109 | APP_DATA['Postal'] = user_location |
| 110 | APP_DATA['City'] = '' |
| 111 | else: |
| 112 | APP_DATA['City'] = user_location |
| 113 | APP_DATA['Postal'] = '' |
| 114 | APP_DATA['Country'] = settings['-country-'] |
| 115 | if settings['-celsius-']: |
| 116 | APP_DATA['Units'] = 'metric' |
| 117 | else: |
| 118 | APP_DATA['Units'] = 'imperial' |
| 119 | |
| 120 | return settings |
| 121 | |
| 122 | |
| 123 | def update_weather(): |
no test coverage detected