Starts and executes the GUI Reads data from a global variable and displays Returns when the user exits / closes the window
()
| 38 | window.write_event_value('-THREAD-', '*** The thread says.... "I am finished" ***') |
| 39 | |
| 40 | def the_gui(): |
| 41 | """ |
| 42 | Starts and executes the GUI |
| 43 | Reads data from a global variable and displays |
| 44 | Returns when the user exits / closes the window |
| 45 | """ |
| 46 | |
| 47 | sg.theme('Light Brown 3') |
| 48 | |
| 49 | layout = [[sg.Text('Long task to perform example')], |
| 50 | [sg.MLine(size=(80, 12), k='-ML-', reroute_stdout=True,write_only=True, autoscroll=True, auto_refresh=True)], |
| 51 | [sg.Text('Number of seconds your task will take'), |
| 52 | sg.Input(key='-SECONDS-', focus=True, size=(5, 1)), |
| 53 | sg.Button('Do Long Task', bind_return_key=True), |
| 54 | sg.CBox('ONE chunk, cannot break apart', key='-ONE CHUNK-')], |
| 55 | [sg.Text('Work progress'), sg.ProgressBar(100, size=(20, 20), orientation='h', key='-PROG-')], |
| 56 | [sg.Button('Click Me'), sg.Button('Exit')], ] |
| 57 | |
| 58 | window = sg.Window('Multithreaded Demonstration Window', layout, finalize=True) |
| 59 | |
| 60 | timeout = thread = None |
| 61 | # --------------------- EVENT LOOP --------------------- |
| 62 | while True: |
| 63 | event, values = window.read(timeout=timeout) |
| 64 | # print(event, values) |
| 65 | if event in (sg.WIN_CLOSED, 'Exit'): |
| 66 | break |
| 67 | elif event.startswith('Do') and not thread: |
| 68 | print('Thread Starting! Long work....sending value of {} seconds'.format(float(values['-SECONDS-']))) |
| 69 | timeout = 100 if values['-ONE CHUNK-'] else None |
| 70 | thread = threading.Thread(target=long_operation_thread, args=(float(values['-SECONDS-']),window), daemon=True) |
| 71 | thread.start() |
| 72 | if values['-ONE CHUNK-']: |
| 73 | sg.popup_animated(sg.DEFAULT_BASE64_LOADING_GIF, background_color='white', transparent_color='white', time_between_frames=100) |
| 74 | elif event == 'Click Me': |
| 75 | print('Your GUI is alive and well') |
| 76 | elif event == '-PROGRESS-': |
| 77 | if not values['-ONE CHUNK-']: |
| 78 | window['-PROG-'].update_bar(values[event], 100) |
| 79 | elif event == '-THREAD-': # Thread has completed |
| 80 | thread.join(timeout=0) |
| 81 | print('Thread finished') |
| 82 | sg.popup_animated(None) # stop animination in case one is running |
| 83 | thread, message, progress, timeout = None, '', 0, None # reset variables for next run |
| 84 | window['-PROG-'].update_bar(0,0) # clear the progress bar |
| 85 | if values['-ONE CHUNK-'] and thread is not None: |
| 86 | sg.popup_animated(sg.DEFAULT_BASE64_LOADING_GIF, background_color='white', transparent_color='white', time_between_frames=100) |
| 87 | window.close() |
| 88 | |
| 89 | |
| 90 | if __name__ == '__main__': |
no test coverage detected