0

i want information in QPlainTextEdit() field. IN THIS CODE IF I RUN ITS ALWAYS STOP AND THEN HANG THE APPLICATION IF I REMOVE STDOUT=PIPE THEN IT WILL RUN PERFECT IN TERMINAL BUT I WANT THAT INFORMATION ON BUTTON CLICK EVENT.

import subprocess

class demo(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

2 Answers2

0

Edited: This may help you to capture the output from CMD,

import subprocess
import tempfile

with tempfile.TemporaryFile() as tempf:
    proc = subprocess.Popen(['whoami'], stdout=tempf)
    proc.wait()
    tempf.seek(0)
    print (tempf.read())

I am executing whoami in the terminal and printing the output

Tamil Selvan
  • 1,600
  • 1
  • 9
  • 25
0

This is the answer and it would be helpfull for others cmd means your command.

import subprocess class demo(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()