From 86a09c2fe0254c94538bd1b0d8e3557f6a70203a Mon Sep 17 00:00:00 2001 From: PySimpleGUI Date: Sat, 17 Sep 2022 10:54:42 -0400 Subject: [PATCH] Better exception handling... keep the app going rather than breaking from event loop. --- DemoPrograms/Demo_Desktop_Widget_Weather.py | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/DemoPrograms/Demo_Desktop_Widget_Weather.py b/DemoPrograms/Demo_Desktop_Widget_Weather.py index 0bc94929..a6c2c0ff 100644 --- a/DemoPrograms/Demo_Desktop_Widget_Weather.py +++ b/DemoPrograms/Demo_Desktop_Widget_Weather.py @@ -280,12 +280,13 @@ def main(refresh_rate, win_location): window = create_window(win_location, settings) - try: - while True: # Event Loop - event, values = window.read(timeout=refresh_in_milliseconds) - if event in (None, '-QUIT-', 'Exit', sg.WIN_CLOSE_ATTEMPTED_EVENT): - sg.user_settings_set_entry('-win location-', window.current_location()) # The line of code to save the position before exiting - break + + while True: # Event Loop + event, values = window.read(timeout=refresh_in_milliseconds) + if event in (None, '-QUIT-', 'Exit', sg.WIN_CLOSE_ATTEMPTED_EVENT): + sg.user_settings_set_entry('-win location-', window.current_location()) # The line of code to save the position before exiting + break + try: if event == '-CHANGE-': x, y = window.current_location() settings = change_settings(settings, (x + 200, y+50)) @@ -303,10 +304,10 @@ def main(refresh_rate, win_location): update_weather() update_metrics(window) - except Exception as e: - sg.Print('*** GOT Exception in event loop ***', c='white on red', location=window.current_location(), keep_on_top=True) - sg.Print('File = ', __file__, f'Window title: {window.Title}') - sg.Print('Exception = ', e, wait=True) # IMPORTANT to add a wait/blocking so that the print pauses execution. Otherwise program continue and exits + except Exception as e: + sg.Print('*** GOT Exception in event loop ***', c='white on red', location=window.current_location(), keep_on_top=True) + sg.Print('File = ', __file__, f'Window title: {window.Title}') + sg.Print('Exception = ', e, wait=True) # IMPORTANT to add a wait/blocking so that the print pauses execution. Otherwise program continue and exits window.close()