Exec APIs - Simplified Subprocesses
Threads, JSON file access, and subprocesses (multiprocessing) are all interfaces that have been simplified within PySimpleGUI. You can use the Python version of these interfaces if you are accustomed to the modules.
There are 4 main types of programs that are launched using the Exec APIs:
- Python files
- Any of any file type
- An editor
- A file explorer
Execute Editor
There are 2 main uses of the Exec APIs launch editor capability
- PySimpleGUI Error window opens your editor to a line of code with an error
- A PySimpleGUI program wants to open the editor
In the PySimpleGUI System Settings you can specify an editor and a command line format string that is used to launch the editor so that it will take you to a specific line. The format of the launch command is needed so that the error window's "Take me to error" feature is able to launch your Python file editor in a way that will move the cursor to the line that has the error.
One example use within a PySimpleGUI program is the Demo Program Browser uses this API call to edit files you've chosen. Another use case is the right-click menu item "Edit Me". This menu item is in many of the Demo Programs so that you can quickly edit the running program without having to locate where it is on your hard drive.
To add the "Edit Me" feature to an application, you can either add a item to a Right-Click menu or use a button to generate an event. Within your event loop, add this code:
Execute Python File
The function execute_py_file
executes a Python file. It does this by calling subprocess.Popen
. The return value is the subprocess id. You will use this id to get the results (output) of the program if you want to read it.
The function parameters control whether or not to wait for the program to complete, the working directory, how to handle the output and error output, the command and optional arguments.
Carefully read the parameter descriptions and look at examples/demo programs to ensure you are calling using the correct settings.
Getting Interpreter
In addition to running a Python file, you can get the interpreter that will be used as well as get the interpreter that is currently running should you wish to force that one be used.
The function execute_py_get_interpreter
returns the interpreter specified in the PySimpleGUI Settings file. If none are specified there, then the currently running interpreter is returned.
For completeness a function is available to get the currently running interpreter. Calling execute_py_get_running_interpreter
returns the value of sys.executable
.
Execute Any File
Executing EXE files, shell scripts, batch files, etc, can be accomplished by calling execute_command_subprocess
. It will run the program by using the subprocess.Popen
call. The return value is the subprocess id. You will use this id to get the results (output) of the program if you want to read it.
The function parameters control whether or not to wait for the program to complete, the working directory, how to handle the output and error output, the command and optional arguments.
Carefully read the parameter descriptions and look at examples/demo programs to ensure you are calling using the correct settings.
Open File Explorer
This is call is used by the Demo Browser. Calling execute_file_explorer(path)
will open the file explorer you specified in the PySimpleGUI System settings to the path passed in as an optional parameter.