()
| 70 | # # # # ### # # |
| 71 | |
| 72 | def main(): |
| 73 | # --------- A Dispatch Dictionary ------- |
| 74 | dispatch_dict = {'Go':do_go, 'Stop':do_stop, (1,2):do_tuple} |
| 75 | |
| 76 | # --------- Define layout and create Window ------- |
| 77 | |
| 78 | layout = [[sg.Text('Dispatching Approaches')], |
| 79 | [sg.Text('Status:'), sg.Text(size=(3, 1), key='-STATUS-')], |
| 80 | [sg.Text(size=(50, 1), key='-OUT-')], |
| 81 | [sg.Button('Simple'), sg.Button('Go'), sg.Button('Stop'), sg.Button('Other', key=do_other), |
| 82 | sg.Button('Tuple', key=(1,2)), sg.Button('Lambda', key= lambda window: do_other(window)), sg.Button('Bad')]] |
| 83 | |
| 84 | window = sg.Window('Dispatchers', layout, font='Default 16', keep_on_top=True) |
| 85 | |
| 86 | # --------- Event Loop ------- |
| 87 | while True: |
| 88 | event, values = window.read() |
| 89 | if event == sg.WIN_CLOSED: # Test for window closed (always do this one) |
| 90 | break |
| 91 | |
| 92 | window['-OUT-'].update(f'Event = {event}') |
| 93 | |
| 94 | # --------- Dispatching of events --------- |
| 95 | # Event processing.... a minimal if-else to show 3 dispatchers |
| 96 | if event == 'Simple': # Dispatch using direct string compare |
| 97 | do_simple(window) |
| 98 | elif callable(event): # Dispatch when event is a function |
| 99 | event(window) |
| 100 | elif event in dispatch_dict: # Dispatch using a dispatch dictionary |
| 101 | func = dispatch_dict.get(event) |
| 102 | func(window) |
| 103 | else: # None of the above |
| 104 | do_not_found(window) |
| 105 | |
| 106 | # --------- After event loop --------- |
| 107 | window.close() |
| 108 | |
| 109 | |
| 110 | if __name__ == '__main__': |
no test coverage detected