我是python中的新手,我花了几个小时在堆栈溢出上搜索解决方案并找到最接近的Issues intercepting subprocess output in real time
我试试这个:
`enter code here` from threading import Thread
from queue import Queue, Empty
def readlines(process, queue):
while process.poll() is None:
queue.put(process.stdout.readline())
...
def startProcess(self):
self.process = subprocess.Popen(['./subtest.sh'],
stdout=subprocess.PIPE,
stdin=subprocess.PIPE,
stderr=subprocess.PIPE)
self.queue = Queue()
self.thread = Thread(target=readlines, args=(self.process, self.queue))
self.thread.start()
self.after(100, self.updateLines)
def updateLines(self):
try:
line = self.queue.get(False) # False for non-blocking, raises Empty if empty
self.console.config(state=tkinter.NORMAL)
self.console.insert(tkinter.END, line)
self.console.config(state=tkinter.DISABLED)
except Empty:
pass
if self.process.poll() is None:
self.after(100, self.updateLines)
然后我发现当subprocess.Popen(py脚本)时,它确实有效。但是当我尝试
subprocess.Popen(C executable
),就像这个subprocess.Popen(["sudo","./xxx","/dev/ttyACMO"
])一样,它没有工作