MCPcopy Index your code
hub / github.com/PySimpleGUI/PySimpleGUI / main

Function main

DemoPrograms/Demo_Multithreaded_Write_Event_Value.py:32–64  ·  view source on GitHub ↗

The demo will display in the multiline info about the event and values dictionary as it is being returned from window.read() Every time "Start" is clicked a new thread is started Try clicking "Dummy" to see that the window is active while the thread stuff is happening in the backgro

()

Source from the content-addressed store, hash-verified

30 # Note that the thread ended event is sent automatically by PySimpleGUI if you started the thread using window.start_thread
31
32def main():
33 """
34 The demo will display in the multiline info about the event and values dictionary as it is being
35 returned from window.read()
36 Every time "Start" is clicked a new thread is started
37 Try clicking "Dummy" to see that the window is active while the thread stuff is happening in the background
38 """
39
40 layout = [ [sg.Text('Output Area - cprint\'s route to here', font='Any 15')],
41 [sg.Multiline(size=(65,20), key='-ML-', autoscroll=True, reroute_stdout=True, write_only=True, reroute_cprint=True)],
42 [sg.T('Input so you can see data in your dictionary')],
43 [sg.Input(key='-IN-', size=(30,1))],
44 [sg.B('Start A Thread', key='-START-'), sg.B('Dummy'), sg.Button('Exit')] ]
45
46 window = sg.Window('Multithreading + Tuple Events', layout)
47
48 while True: # Event Loop
49 event, values = window.read()
50 sg.cprint(event, values)
51 if event == sg.WIN_CLOSED or event == 'Exit':
52 break
53 if event == '-START-':
54 window.start_thread(lambda : the_thread(window), ('-THREAD-', '-ENDED-'))
55 if event[0] == '-THREAD-':
56 if event[1] == '-PRINT-':
57 sg.cprint(f'Data from the thread ', colors='white on purple', end='')
58 sg.cprint(f'{values[event]}', colors='white on red')
59 elif event[1] == '-ENDED-':
60 sg.cprint('Thread has ended', colors='white on blue')
61 elif event[1] == '-STARTED-':
62 sg.cprint('Thread has started', colors='white on green')
63
64 window.close()
65
66
67if __name__ == '__main__':

Calls 4

readMethod · 0.95
start_threadMethod · 0.95
closeMethod · 0.95
the_threadFunction · 0.70

Tested by

no test coverage detected