Merge pull request #5612 from PySimpleGUI/Dev-latest
Several changes - exception handling if problems with weather API. R…
This commit is contained in:
		
						commit
						c0e2d8ab98
					
				
					 1 changed files with 42 additions and 41 deletions
				
			
		|  | @ -75,8 +75,9 @@ def change_settings(settings, window_location=(None, None)): | |||
|         nearest_postal = '' | ||||
| 
 | ||||
|     layout = [[sg.T('Enter Zipcode or City for your location')], | ||||
|               [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('-location-', nearest_postal), size=(15, 1), key='-LOCATION-'), sg.T('City')], | ||||
|               [sg.I(settings.get('-country-', 'US'), size=(15, 1), key='-COUNTRY-'), sg.T('Country')], | ||||
|               [sg.I(settings.get('-friends name-', ''), size=(15, 1), key='-FRIENDS NAME-'), sg.T('Who')], | ||||
|               [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)], ] | ||||
|  | @ -96,6 +97,7 @@ def change_settings(settings, window_location=(None, None)): | |||
|         settings['-country-'] = values['-COUNTRY-'] | ||||
|         API_KEY = settings['-api key-'] = values['-API KEY-'] | ||||
|         settings['-celsius-'] = values['-CELSIUS-'] | ||||
|         settings['-friends name-'] = values['-FRIENDS NAME-'] | ||||
|     else: | ||||
|         API_KEY = settings['-api key-'] | ||||
|         user_location = settings['-location-'] | ||||
|  | @ -186,47 +188,46 @@ def metric_row(metric): | |||
|             sg.Text(APP_DATA[metric], font=('Arial', 10, 'bold'), pad=(0, 0), size=(9, 1), key=metric)] | ||||
| 
 | ||||
| 
 | ||||
| def create_window(win_location): | ||||
| def create_window(win_location, settings): | ||||
|     """ Create the application window """ | ||||
|     friends_name = settings.get('-friends name-', '') | ||||
|     col1 = sg.Column( | ||||
|         [[sg.Text(APP_DATA['City'], font=('Arial Rounded MT Bold', 18), pad=((10, 0), (50, 0)), size=(18, 1), background_color=BG_COLOR, text_color=TXT_COLOR, key='City')], | ||||
|         [[sg.Text(APP_DATA['City'], font=('Arial Rounded MT Bold', 18),   background_color=BG_COLOR, text_color=TXT_COLOR, key='City'), | ||||
|           sg.Text(f' - {friends_name}' if friends_name else '',  background_color=BG_COLOR, text_color=TXT_COLOR, font=('Arial Rounded MT Bold', 18),)], | ||||
|          [sg.Text(APP_DATA['Description'], font=('Arial', 12), pad=(10, 0), background_color=BG_COLOR, text_color=TXT_COLOR, key='Description')]], | ||||
|         background_color=BG_COLOR, key='COL1') | ||||
| 
 | ||||
|     col2 = sg.Column( | ||||
|         [[sg.Text('×', font=('Arial Black', 16), pad=(0, 0), justification='right', background_color=BG_COLOR, text_color=TXT_COLOR, enable_events=True, key='-QUIT-')], | ||||
|          [sg.Image(data=APP_DATA['Icon'], pad=((5, 10), (0, 0)), size=(100, 100), background_color=BG_COLOR, key='Icon')]], | ||||
|         element_justification='center', background_color=BG_COLOR, key='COL2') | ||||
|     col2 = sg.Column([[sg.Image(data=APP_DATA['Icon'], size=(100, 100), background_color=BG_COLOR, key='Icon')]], | ||||
|                      element_justification='center', background_color=BG_COLOR, key='COL2') | ||||
| 
 | ||||
|     col3 = sg.Column( | ||||
|         [[sg.Text(APP_DATA['Updated'], font=('Arial', 8), background_color=BG_COLOR, text_color=TXT_COLOR, key='Updated')]], | ||||
|         pad=(10, 5), element_justification='left', background_color=BG_COLOR, key='COL3') | ||||
|     col3 = sg.Column([[sg.Text(APP_DATA['Updated'], font=('Arial', 8), background_color=BG_COLOR, text_color=TXT_COLOR, key='Updated')]], | ||||
|                       pad=(10, 5), element_justification='left', background_color=BG_COLOR, key='COL3') | ||||
| 
 | ||||
|     col4 = sg.Column( | ||||
|         [[sg.Text('Settings', font=('Arial', 8, 'italic'), background_color=BG_COLOR, text_color=TXT_COLOR, enable_events=True, key='-CHANGE-'), | ||||
|           sg.Text('Refresh', font=('Arial', 8, 'italic'), background_color=BG_COLOR, text_color=TXT_COLOR, enable_events=True, key='-REFRESH-')]], | ||||
|         pad=(10, 5), element_justification='right', background_color=BG_COLOR, key='COL4') | ||||
| 
 | ||||
|     top_col = sg.Column([[col1, col2]], pad=(0, 0), background_color=BG_COLOR, key='TopCOL') | ||||
|     top_col = sg.Column([[col1, sg.Push(background_color=BG_COLOR), col2, sg.Text('×', font=('Arial Black', 16), pad=(0, 0), justification='right', background_color=BG_COLOR, text_color=TXT_COLOR, enable_events=True, key='-QUIT-')]],                           pad=(0, 0), background_color=BG_COLOR, key='TopCOL') | ||||
| 
 | ||||
|     bot_col = sg.Column([[col3, col4]], pad=(0, 0), background_color=BG_COLOR, key='BotCOL') | ||||
|     bot_col = sg.Column([[col3, col4]], | ||||
|                         pad=(0, 0), background_color=BG_COLOR, key='BotCOL') | ||||
| 
 | ||||
|     lf_col = sg.Column( | ||||
|         [[sg.Text(APP_DATA['Temp'], font=('Haettenschweiler', 90), pad=((10, 0), (0, 0)), justification='center', key='Temp')]], | ||||
|         pad=(10, 0), element_justification='center', key='LfCOL') | ||||
| 
 | ||||
|     rt_col = sg.Column( | ||||
|         [metric_row('Feels Like'), metric_row('Wind'), metric_row('Humidity'), metric_row('Precip 1hr'), metric_row('Pressure')], | ||||
|         pad=((15, 0), (25, 5)), key='RtCOL') | ||||
|     rt_col = sg.Column([metric_row('Feels Like'), metric_row('Wind'), metric_row('Humidity'), metric_row('Precip 1hr'), metric_row('Pressure')], | ||||
|                         pad=((15, 0), (25, 5)), key='RtCOL') | ||||
| 
 | ||||
|     layout = [[top_col], | ||||
|               [lf_col, rt_col], | ||||
|               [bot_col], | ||||
|               [sg.Text(f'{sg.ver} {sg.framework_version} {sys.version}', font=('Arial', 8), background_color=BG_COLOR, text_color=TXT_COLOR, pad=(0,0))]] | ||||
|               [sg.Text(f'{sg.ver} {sg.framework_version} {sys.version}', font=('Arial', 8), justification='c', background_color=BG_COLOR, text_color=TXT_COLOR, pad=(0,0), expand_x=True)]] | ||||
| 
 | ||||
|     window = sg.Window(layout=layout, title='Weather Widget', margins=(0, 0), finalize=True, location=win_location, | ||||
|                        element_justification='center', keep_on_top=True, no_titlebar=True, grab_anywhere=True, alpha_channel=ALPHA, | ||||
|                        right_click_menu=[[''], ['Edit Me', 'Versions', 'Settings', 'Exit',]], enable_close_attempted_event=True) | ||||
|                        right_click_menu=[[''], ['Edit Me', 'Versions', 'Exit',]], enable_close_attempted_event=True) | ||||
| 
 | ||||
|     for col in ['COL1', 'COL2', 'TopCOL', 'BotCOL', '-QUIT-']: | ||||
|         window[col].expand(expand_y=True, expand_x=True) | ||||
|  | @ -277,30 +278,30 @@ def main(refresh_rate, win_location): | |||
|         sg.popup_error('Having trouble with location.  Your location: ', location) | ||||
|         exit() | ||||
| 
 | ||||
|     window = create_window(win_location) | ||||
|     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 | ||||
|             if event in ('-CHANGE-', 'Settings'): | ||||
|                 x, y = window.current_location() | ||||
|                 settings = change_settings(settings, (x + 200, y+50)) | ||||
|             elif event == '-REFRESH-': | ||||
|                 sg.popup_quick_message('Refreshing...', keep_on_top=True, background_color='red', text_color='white', | ||||
|                                        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)) | ||||
|             elif event == 'Edit Me': | ||||
|                 sg.execute_editor(__file__) | ||||
|             elif event == 'Versions': | ||||
|                 sg.popup_scrolled(__file__, sg.get_versions(), keep_on_top=True, location=window.current_location()) | ||||
|             elif event != sg.TIMEOUT_KEY: | ||||
|                 sg.Print('Unknown event received\nEvent & values:\n', event, values, location=win_location, keep_on_top=True) | ||||
|     window = create_window(win_location, settings) | ||||
| 
 | ||||
|             update_weather() | ||||
|             update_metrics(window) | ||||
|     except Exception as e: | ||||
|         sg.Print('Exception in Weather Widget event loop', sg.__file__, e, keep_on_top=True, wait=True, location=win_location) | ||||
|     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 | ||||
|         if event == '-CHANGE-': | ||||
|             x, y = window.current_location() | ||||
|             settings = change_settings(settings, (x + 200, y+50)) | ||||
|             window.close() | ||||
|             window = create_window(win_location, settings) | ||||
|         elif event == '-REFRESH-': | ||||
|             sg.popup_quick_message('Refreshing...', keep_on_top=True, background_color='red', text_color='white', | ||||
|                                    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)) | ||||
|         elif event == 'Edit Me': | ||||
|             sg.execute_editor(__file__) | ||||
|         elif event == 'Versions': | ||||
|             sg.main_get_debug_data() | ||||
|         elif event != sg.TIMEOUT_KEY: | ||||
|             sg.Print('Unknown event received\nEvent & values:\n', event, values, location=win_location) | ||||
| 
 | ||||
|         update_weather() | ||||
|         update_metrics(window) | ||||
|     window.close() | ||||
| 
 | ||||
| 
 | ||||
|  |  | |||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue