Switched to using the PySimpleGUI supplied threading capability instead of manually starting the thread

This commit is contained in:
PySimpleGUI 2022-03-28 18:27:50 -04:00
parent 745f26916d
commit 53230912a8
1 changed files with 6 additions and 4 deletions

View File

@ -3,7 +3,6 @@ import numpy as np
from matplotlib.backends.backend_tkagg import FigureCanvasAgg from matplotlib.backends.backend_tkagg import FigureCanvasAgg
import matplotlib.pyplot as plt import matplotlib.pyplot as plt
import io import io
import threading
import time import time
""" """
@ -25,7 +24,7 @@ import time
The example static graph can be found in the matplotlib gallery: The example static graph can be found in the matplotlib gallery:
https://matplotlib.org/stable/gallery/images_contours_and_fields/specgram_demo.html https://matplotlib.org/stable/gallery/images_contours_and_fields/specgram_demo.html
Copyright 2021 PySimpleGUI Copyright 2021, 2022 PySimpleGUI
""" """
np.random.seed(19801) np.random.seed(19801)
@ -141,7 +140,7 @@ def main():
# define the window layout # define the window layout
layout = [[sg.Text('Spectrogram Animated - Threaded', font='Helvetica 24')], layout = [[sg.Text('Spectrogram Animated - Threaded', font='Helvetica 24')],
[sg.pin(sg.Image(key='-IMAGE-'))], [sg.pin(sg.Image(key='-IMAGE-'))],
[sg.T(size=(50, 1), k='-STATS-')], [sg.T(k='-STATS-')],
[sg.B('Animate', focus=True, k='-ANIMATE-')]] [sg.B('Animate', focus=True, k='-ANIMATE-')]]
# create the form and show it without the plot # create the form and show it without the plot
@ -156,7 +155,7 @@ def main():
if event == '-ANIMATE-': if event == '-ANIMATE-':
window['-IMAGE-'].update(visible=True) window['-IMAGE-'].update(visible=True)
start_time = time.time() start_time = time.time()
threading.Thread(target=the_thread, args=(window,), daemon=True).start() window.start_thread(lambda: the_thread(window), '-THEAD FINISHED-')
elif event == '-THREAD-': elif event == '-THREAD-':
plt.close('all') # close all plots... unclear if this is required plt.close('all') # close all plots... unclear if this is required
window['-IMAGE-'].update(data=values[event].read()) window['-IMAGE-'].update(data=values[event].read())
@ -170,4 +169,7 @@ def main():
if __name__ == '__main__': if __name__ == '__main__':
# Newer versions of PySimpleGUI have an alias for this method that's called "start_thread" so that it's clearer what's happening
# In case you don't have that version installed this line of code creates the alias for you
sg.Window.start_thread = sg.Window.perform_long_operation
main() main()