如何存储来自 subprocess.popen 的实时信息并显示在 QPlainTextEdit 中
how can i store the live information from subprocess.popen and display in QPlainTextEdit
我想要 QPlainTextEdit() 字段中的信息。
在此代码中,如果我 运行 它总是停止然后挂起应用程序
如果我删除 STDOUT=PIPE,那么它将 运行 在终端中完美显示,但我想要有关按钮单击事件的信息。
导入子进程
class 演示(QWidget)
def __init__(self):
QWidget.__init__(self)
self.program_output = QPlainTextEdit()
self.program_output.setReadOnly(True)
self.program_output.setCenterOnScroll(True)
self.decode_btn = ToggleButton('Decode')
self.decode_btn.clicked.connect(self.decode_scan)
@pyqtSlot(bool)
def decode_scan(self, checked):
print('Decoder Click')
self.decode_btn.setChecked(checked)
if checked:
self.process_timer.stop()
#self.pre_process()
print('About to run decoder:')
output = subprocess.Popen('./decoder', cwd='/mnt/encrypted_fs/scripts/decode', stdout=PIPE)
response = output.communicate()
print (response)
sleep(1)
self.process_timer.start()
else:
print('Decoder button not clicked')
if output:
subprocess.Popen(f'sudo kill -9 {self.info.pid+1}', shell=True).wait()
output = None
已编辑:这可以帮助您捕获 CMD 的输出,
import subprocess
import tempfile
with tempfile.TemporaryFile() as tempf:
proc = subprocess.Popen(['whoami'], stdout=tempf)
proc.wait()
tempf.seek(0)
print (tempf.read())
我正在终端中执行 whoami
并打印输出
这就是答案,对其他人会有帮助
cmd 表示您的命令。
导入子进程
class 演示(QWidget)
@pyqtSlot(bool)
def decode_scan(self, checked):
print('Decoder Click')
self.decode_btn.setChecked(checked)
if checked:
self.process_timer.stop()
#self.pre_process()
print('About to run decoder:')
self.proc = subprocess.Popen(['cmd'], stderr=subprocess.STDOUT,stdout=subprocess.PIPE)
#proc.wait()
for line in iter(self.proc.stdout.readline, b''):
sys.stdout.write(line.decode(sys.stdout.encoding))
sleep(1)
self.process_timer.start()
else:
print('Decoder button not clicked')
if self.proc:
subprocess.Popen(f'sudo kill -9 {self.proc.pid+1}', shell=True).wait()
我想要 QPlainTextEdit() 字段中的信息。 在此代码中,如果我 运行 它总是停止然后挂起应用程序 如果我删除 STDOUT=PIPE,那么它将 运行 在终端中完美显示,但我想要有关按钮单击事件的信息。
导入子进程
class 演示(QWidget)
def __init__(self):
QWidget.__init__(self)
self.program_output = QPlainTextEdit()
self.program_output.setReadOnly(True)
self.program_output.setCenterOnScroll(True)
self.decode_btn = ToggleButton('Decode')
self.decode_btn.clicked.connect(self.decode_scan)
@pyqtSlot(bool)
def decode_scan(self, checked):
print('Decoder Click')
self.decode_btn.setChecked(checked)
if checked:
self.process_timer.stop()
#self.pre_process()
print('About to run decoder:')
output = subprocess.Popen('./decoder', cwd='/mnt/encrypted_fs/scripts/decode', stdout=PIPE)
response = output.communicate()
print (response)
sleep(1)
self.process_timer.start()
else:
print('Decoder button not clicked')
if output:
subprocess.Popen(f'sudo kill -9 {self.info.pid+1}', shell=True).wait()
output = None
已编辑:这可以帮助您捕获 CMD 的输出,
import subprocess
import tempfile
with tempfile.TemporaryFile() as tempf:
proc = subprocess.Popen(['whoami'], stdout=tempf)
proc.wait()
tempf.seek(0)
print (tempf.read())
我正在终端中执行 whoami
并打印输出
这就是答案,对其他人会有帮助 cmd 表示您的命令。
导入子进程 class 演示(QWidget)
@pyqtSlot(bool)
def decode_scan(self, checked):
print('Decoder Click')
self.decode_btn.setChecked(checked)
if checked:
self.process_timer.stop()
#self.pre_process()
print('About to run decoder:')
self.proc = subprocess.Popen(['cmd'], stderr=subprocess.STDOUT,stdout=subprocess.PIPE)
#proc.wait()
for line in iter(self.proc.stdout.readline, b''):
sys.stdout.write(line.decode(sys.stdout.encoding))
sleep(1)
self.process_timer.start()
else:
print('Decoder button not clicked')
if self.proc:
subprocess.Popen(f'sudo kill -9 {self.proc.pid+1}', shell=True).wait()