Skip to content

Redirect subprocess Output to Text Area #681

Answered by vsquared
vsquared asked this question in Q&A
Discussion options

You must be logged in to vote

This should be the solution. Launch_thread is your friend. The runProcess will work correctly if run on a separate thread:

# Uses Imported mode for py5

from java.awt import *
from javax.swing import *
import threading
import subprocess
import os
import io

_wndW = 750
_wndH = 200

def runProcess():
  global logArea,process,frame,wnd
  cmdStr = "/opt/miniconda3/bin/py5-run-sketch"
  filePath = "/Users/xxxx/py5_code/Arrows2.py"
  process = subprocess.Popen([cmdStr,filePath], stdout=subprocess.PIPE, text=True)
  while True:
    outStr = process.stdout.readline()
    logArea.append(outStr)
    if not outStr:
        break
    print(outStr.strip())
#     process.wait() # Wait for the process …

Replies: 2 comments

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Answer selected by vsquared
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
1 participant