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

Function the_gui

DemoPrograms/Demo_Multithreaded_Multiple_Threads.py:59–104  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

57
58############################# Begin GUI code #############################
59def the_gui():
60 sg.theme('Light Brown 3')
61
62
63 layout = [[sg.Text('Multithreaded Work Example')],
64 [sg.Text('Click Go to start a long-running function call')],
65 [sg.Text(size=(25, 1), key='-OUTPUT-')],
66 [sg.Text(size=(25, 1), key='-OUTPUT2-')],
67 [sg.Text('⚫', text_color='blue', key=i, pad=(0,0), font='Default 14') for i in range(20)],
68 [sg.Button('Go'), sg.Button('Popup'), sg.Button('Exit')], ]
69
70 window = sg.Window('Multithreaded Window', layout)
71 # --------------------- EVENT LOOP ---------------------
72 work_id = 0
73 while True:
74 # wait for up to 100 ms for a GUI event
75 event, values = window.read()
76 if event in (sg.WIN_CLOSED, 'Exit'):
77 break
78 if event == 'Go': # clicking "Go" starts a long running work item by starting thread
79 window['-OUTPUT-'].update('Starting long work %s' % work_id)
80 window[work_id].update(text_color='red')
81 # LOCATION 2
82 # STARTING long run by starting a thread
83 thread_id = threading.Thread(
84 target=long_function_wrapper,
85 args=(work_id, window,),
86 daemon=True)
87 thread_id.start()
88 work_id = work_id+1 if work_id < 19 else 0
89
90 # if message received from queue, then some work was completed
91 if event == '-THREAD DONE-':
92 # LOCATION 3
93 # this is the place you would execute code at ENDING of long running task
94 # You can check the completed_work_id variable
95 # to see exactly which long-running function completed
96 completed_work_id = values[event]
97 window['-OUTPUT2-'].update(
98 'Complete Work ID "{}"'.format(completed_work_id))
99 window[completed_work_id].update(text_color='green')
100
101 if event == 'Popup':
102 sg.popup_non_blocking('This is a popup showing that the GUI is running', grab_anywhere=True)
103 # if user exits the window, then close the window and exit the GUI func
104 window.close()
105
106############################# Main #############################
107

Calls 4

readMethod · 0.95
closeMethod · 0.95
startMethod · 0.80
updateMethod · 0.45

Tested by

no test coverage detected