The main program routine
(refresh_rate, win_location)
| 256 | |
| 257 | |
| 258 | def main(refresh_rate, win_location): |
| 259 | """ The main program routine """ |
| 260 | refresh_in_milliseconds = refresh_rate * 60 * 1000 |
| 261 | |
| 262 | # Load settings from config file. If none found will create one |
| 263 | settings = load_settings() |
| 264 | location = settings['-location-'] |
| 265 | APP_DATA['Country'] = settings.get('-country-', 'US') |
| 266 | if settings.get('-celsius-'): |
| 267 | APP_DATA['Units'] = 'metric' |
| 268 | else: |
| 269 | APP_DATA['Units'] = 'imperial' |
| 270 | |
| 271 | if location is not None: |
| 272 | if location.isnumeric() and len(location) == 5 and location is not None: |
| 273 | APP_DATA['Postal'] = location |
| 274 | APP_DATA['City'] = '' |
| 275 | else: |
| 276 | APP_DATA['City'] = location |
| 277 | APP_DATA['Postal'] = '' |
| 278 | update_weather() |
| 279 | else: |
| 280 | sg.popup_error('Having trouble with location. Your location: ', location) |
| 281 | exit() |
| 282 | |
| 283 | window = create_window(win_location, settings) |
| 284 | |
| 285 | |
| 286 | while True: # Event Loop |
| 287 | event, values = window.read(timeout=refresh_in_milliseconds) |
| 288 | if event in (None, '-QUIT-', 'Exit', sg.WIN_CLOSE_ATTEMPTED_EVENT): |
| 289 | sg.user_settings_set_entry('-win location-', window.current_location()) # The line of code to save the position before exiting |
| 290 | break |
| 291 | try: |
| 292 | if event == '-CHANGE-': |
| 293 | x, y = window.current_location() |
| 294 | settings = change_settings(settings, (x + 200, y+50)) |
| 295 | window.close() |
| 296 | window = create_window(win_location, settings) |
| 297 | elif event == '-REFRESH-': |
| 298 | sg.popup_quick_message('Refreshing...', keep_on_top=True, background_color='red', text_color='white', |
| 299 | auto_close_duration=3, non_blocking=False, location=(window.current_location()[0]+window.size[0]//2-30, window.current_location()[1]+window.size[1]//2-10)) |
| 300 | elif event == 'Edit Me': |
| 301 | sg.execute_editor(__file__) |
| 302 | elif event == 'Versions': |
| 303 | sg.main_get_debug_data() |
| 304 | elif event != sg.TIMEOUT_KEY: |
| 305 | sg.Print('Unknown event received\nEvent & values:\n', event, values, location=win_location) |
| 306 | |
| 307 | update_weather() |
| 308 | update_metrics(window) |
| 309 | except Exception as e: |
| 310 | sg.Print('*** GOT Exception in event loop ***', c='white on red', location=window.current_location(), keep_on_top=True) |
| 311 | sg.Print('File = ', __file__, f'Window title: {window.Title}') |
| 312 | sg.Print('Exception = ', e, wait=True) # IMPORTANT to add a wait/blocking so that the print pauses execution. Otherwise program continue and exits |
| 313 | window.close() |
| 314 | |
| 315 |
no test coverage detected