From 5b89f087148d618ab6e13b50ebf3ce80c812c79e Mon Sep 17 00:00:00 2001 From: PySimpleGUI Date: Mon, 24 Feb 2020 09:10:42 -0500 Subject: [PATCH] New multithreaded demo --- ...mo_Multithreaded_Animated_Shell_Command.py | 36 +++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 DemoPrograms/Demo_Multithreaded_Animated_Shell_Command.py diff --git a/DemoPrograms/Demo_Multithreaded_Animated_Shell_Command.py b/DemoPrograms/Demo_Multithreaded_Animated_Shell_Command.py new file mode 100644 index 00000000..3b3ea9e4 --- /dev/null +++ b/DemoPrograms/Demo_Multithreaded_Animated_Shell_Command.py @@ -0,0 +1,36 @@ +import subprocess +import PySimpleGUI as sg +import threading + +""" + Demo - Run a shell command while displaying an animated GIF to inform the user the + program is still running. + If you have a GUI and you start a subprocess to run a shell command, the GUI essentually + locks up and often the operation system will off to terminate the program for you. + + This demo fixes this situation by running the subprocess as a Thread. This enables + the subproces to run async to the main program. The main program then simply runs a loop, + waiting for the thread to complete running. + + The output from the subprocess is saved and displayed in a scrolled popup. +""" + + +def process_thread(): + global proc + proc = subprocess.run('pip list', shell=True, stdout=subprocess.PIPE) + + +thread = threading.Thread(target=process_thread, daemon=True) +thread.start() + +while True: + sg.popup_animated(sg.DEFAULT_BASE64_LOADING_GIF, 'Loading list of packages', time_between_frames=100) + thread.join(timeout=.1) + if not thread.is_alive(): + break +sg.popup_animated(None) + +output = proc.__str__().replace('\\r\\n', '\n') +sg.popup_scrolled(output) +