Where execution begins The Event Loop lives here, but the window creation is done in another function This is an important design pattern :param location: Location to create the main window if one is not found in the user settings :type location: Tuple[int, int]
(location)
| 124 | |
| 125 | |
| 126 | def main(location): |
| 127 | """ |
| 128 | Where execution begins |
| 129 | The Event Loop lives here, but the window creation is done in another function |
| 130 | This is an important design pattern |
| 131 | |
| 132 | :param location: Location to create the main window if one is not found in the user settings |
| 133 | :type location: Tuple[int, int] |
| 134 | """ |
| 135 | |
| 136 | window = make_window(sg.user_settings_get_entry('-location-', location)) |
| 137 | |
| 138 | refresh_frequency = sg.user_settings_get_entry('-fresh frequency-', UPDATE_FREQUENCY_MILLISECONDS) |
| 139 | |
| 140 | while True: # Event Loop |
| 141 | # Normally a window.read goes here, but first we're updating the values in the window, then reading it |
| 142 | # First update the status information |
| 143 | window['-MAIN INFO-'].update('Your Info') |
| 144 | # for debugging show the last update date time |
| 145 | window['-REFRESHED-'].update(datetime.datetime.now().strftime("%m/%d/%Y\n%I:%M:%S %p")) |
| 146 | |
| 147 | # -------------- Start of normal event loop -------------- |
| 148 | event, values = window.read(timeout=refresh_frequency) |
| 149 | print(event, values) |
| 150 | if event in (sg.WIN_CLOSED, 'Exit'): # standard exit test... ALWAYS do this |
| 151 | break |
| 152 | if event == 'Edit Me': |
| 153 | sg.execute_editor(__file__) |
| 154 | elif event == 'Choose Title': |
| 155 | new_title = sg.popup_get_text('Choose a title for your Widget', location=window.current_location(), keep_on_top=True) |
| 156 | if new_title is not None: |
| 157 | window['-TITLE-'].update(new_title) |
| 158 | sg.user_settings_set_entry('-title-', new_title) |
| 159 | elif event == 'Show Refresh Info': |
| 160 | window['-REFRESHED-'].update(visible=True) |
| 161 | sg.user_settings_set_entry('-show refresh-', True) |
| 162 | elif event == 'Save Location': |
| 163 | sg.user_settings_set_entry('-location-', window.current_location()) |
| 164 | elif event == 'Hide Refresh Info': |
| 165 | window['-REFRESHED-'].update(visible=False) |
| 166 | sg.user_settings_set_entry('-show refresh-', False) |
| 167 | elif event in [str(x) for x in range(1, 11)]: # if Alpha Channel was chosen |
| 168 | window.set_alpha(int(event) / 10) |
| 169 | sg.user_settings_set_entry('-alpha-', int(event) / 10) |
| 170 | elif event == 'Set Refresh Rate': |
| 171 | choice = sg.popup_get_text('How frequently to update window in seconds? (can be a float)', default_text=sg.user_settings_get_entry('-fresh frequency-', UPDATE_FREQUENCY_MILLISECONDS)/1000, location=window.current_location(), keep_on_top=True) |
| 172 | if choice is not None: |
| 173 | try: |
| 174 | refresh_frequency = float(choice)*1000 # convert to milliseconds |
| 175 | sg.user_settings_set_entry('-fresh frequency-', float(refresh_frequency)) |
| 176 | except Exception as e: |
| 177 | sg.popup_error(f'You entered an incorrect number of seconds: {choice}', f'Error: {e}', location=window.current_location(), keep_on_top=True) |
| 178 | elif event == 'New Theme': |
| 179 | loc = window.current_location() |
| 180 | if choose_theme(window.current_location(), window.size) is not None: |
| 181 | window.close() # out with the old... |
| 182 | window = make_window(loc) # in with the new |
| 183 |
no test coverage detected