Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Redirect a Python Console output to a QTextBox

I'm working on developing a GUI for the recompilation of Linux kernel. For this I need to implement 4-5 Linux commands from Python. I use Qt as GUI designer. I have successfully implemented the commands using os.system() call. But the output is obtained at the console. The real problem is the output of command is a listing that takes almost 20-25 min continuous printing. How we can transfer this console output to a text box designed in Qt. Can any one help me to implement the setSource() operation in Qt using source as the live console outputs.

like image 766
Kris Avatar asked Feb 28 '23 00:02

Kris


2 Answers

self.process = QProcess()
self.connect(self.process, SIGNAL("readyReadStdout()"), self.readOutput)
self.connect(self.process, SIGNAL("readyReadStderr()"), self.readErrors)
tarsourcepath="sudo tar xvpf "+ self.path1
self.process.setArguments(QStringList.split(" ",tarsourcepath))
self.process.start()



def readOutput(self):

    self.textBrowser2.append(QString(self.process.readStdout()))
    if self.process.isRunning()==False:
        self.textBrowser2.append("\n Completed Successfully")




def readErrors(self):
    self.textBrowser2.append("error: " + QString(self.process.readLineStderr()))

This did the work quite good for me. thank you all.

like image 62
Kris Avatar answered Mar 07 '23 12:03

Kris


I mostly deal with wxPython, but is http://diotavelli.net/PyQtWiki/Capturing_Output_from_a_Process a solution that would work?

From the page:

Problem: You want to run a process that prints lots of information to the console and display the output in a text editor or browser, but the result is a GUI that freezes until the process is finished.

Solution (one of many possible): Create a QProcess object, connect its signals to some slots in your class, pass it the required arguments and start it. Data on the process's stdout and stderr is delivered to your slots.

continued...

like image 44
Nick T Avatar answered Mar 07 '23 11:03

Nick T