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

Function main

DemoPrograms/Demo_Multithreaded_Signal_Thread_To_End.py:42–81  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

40# dP dP dP `88888P8 dP dP dP
41
42def main():
43
44 layout = [
45 [sg.Text('00:00:00', font=('Courier New', 20, 'bold'), justification='center', expand_x=True, key='-TIME-')],
46 [sg.Push(), sg.Button('Start'), sg.Button('Stop')]]
47
48 window = sg.Window('Threading', layout, enable_close_attempted_event=True,
49 print_event_values=True, # enable to watch the events and values print out
50 )
51
52 window.job_running = False # Create a member variable to signal to the thread when to stop
53 exiting = False # Used when X is clicked
54
55 while True:
56
57 event, values = window.read()
58
59 if event == sg.WINDOW_CLOSE_ATTEMPTED_EVENT:
60 if window.job_running: # if thread running, tell it to exit
61 window.job_running = False
62 exiting = True
63 else:
64 break # if thread not running then OK to exit
65
66 if exiting and event == '-THREAD ENDED-': # If exiting and thread is finished then OK to exit
67 break
68
69 elif event == 'Start':
70 window['Start'].update(disabled=True)
71 window.job_running = True
72 window.start_thread(lambda: the_thread(window), '-THREAD ENDED-')
73
74 elif event == 'Stop':
75 window.job_running = False
76 window['Start'].update(disabled=False)
77
78 elif event == '-THREAD-' and values[event] != window['-TIME-'].get():
79 window['-TIME-'].update(values[event])
80
81 window.close()
82
83if __name__ == '__main__':
84 main()

Calls 6

readMethod · 0.95
start_threadMethod · 0.95
closeMethod · 0.95
the_threadFunction · 0.70
updateMethod · 0.45
getMethod · 0.45

Tested by

no test coverage detected