Starts and executes the GUI Reads data from a Queue and displays the data to the window Returns when the user exits / closes the window (that means it does NOT return until the user exits the window) :param gui_queue: Queue the GUI should read from :return:
()
| 94 | |
| 95 | |
| 96 | def main(): |
| 97 | """ |
| 98 | Starts and executes the GUI |
| 99 | Reads data from a Queue and displays the data to the window |
| 100 | Returns when the user exits / closes the window |
| 101 | (that means it does NOT return until the user exits the window) |
| 102 | :param gui_queue: Queue the GUI should read from |
| 103 | :return: |
| 104 | """ |
| 105 | layout = [[sg.Text('Multithreaded Window Example')], |
| 106 | [sg.Text('', size=(15, 1), key='-OUTPUT-')], |
| 107 | [sg.Multiline(size=(40, 26), key='-ML-', autoscroll=True)], |
| 108 | [sg.Push(), sg.Button('Exit')], ] |
| 109 | |
| 110 | window = sg.Window('Multithreaded Window', layout, finalize=True) |
| 111 | |
| 112 | # -- Create a Queue to communicate with GUI -- |
| 113 | # queue used to communicate between the gui and the threads |
| 114 | # -- Start worker threads, each taking a different amount of time |
| 115 | window.start_thread(lambda: worker_thread1('Thread 1', 500, window)) |
| 116 | window.start_thread(lambda: worker_thread2('Thread 2', 200, window)) |
| 117 | window.start_thread(lambda: worker_thread3('Thread 3', 1000, window)) |
| 118 | # -- Start the GUI passing in the Queue -- |
| 119 | |
| 120 | sg.cprint_set_output_destination(window, '-ML-') |
| 121 | |
| 122 | colors = {'Thread 1':('white', 'red'), 'Thread 2':('white', 'purple'), 'Thread 3':('white', 'blue')} |
| 123 | # --------------------- EVENT LOOP --------------------- |
| 124 | while True: |
| 125 | # wait for up to 100 ms for a GUI event |
| 126 | event, values = window.read() |
| 127 | if event in (sg.WIN_CLOSED, 'Exit'): |
| 128 | break |
| 129 | # --------------- Loop through all messages coming in from threads --------------- |
| 130 | sg.cprint(event, values[event], c=colors[event]) |
| 131 | # if user exits the window, then close the window and exit the GUI func |
| 132 | window.close() |
| 133 | |
| 134 | |
| 135 | if __name__ == '__main__': |
no test coverage detected