-
-
Save limkokhole/3b94eee474b465531c34cdb81c16148c to your computer and use it in GitHub Desktop.
Redirect both stdin and stdout of a process to a PyQt text edit.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from PyQt4 import QtGui, QtCore, uic | |
def p(x): | |
print x | |
class MainWindow(QtGui.QMainWindow): | |
def __init__(self): | |
QtGui.QWidget.__init__(self) | |
uic.loadUi('redirect.ui', self) | |
print 'Connecting process' | |
self.process = QtCore.QProcess(self) | |
self.process.readyReadStandardOutput.connect(self.stdoutReady) | |
self.process.readyReadStandardError.connect(self.stderrReady) | |
self.process.started.connect(lambda: p('Started!')) | |
self.process.finished.connect(lambda: p('Finished!')) | |
print 'Starting process' | |
self.process.start('python', ['speak.py']) | |
def append(self, text): | |
cursor = self.textEdit.textCursor() | |
cursor.movePosition(cursor.End) | |
cursor.insertText(text) | |
#self.output.ensureCursorVisible() | |
def stdoutReady(self): | |
text = str(self.process.readAllStandardOutput()) | |
print text.strip() | |
self.append(text) | |
def stderrReady(self): | |
text = str(self.process.readAllStandardError()) | |
print text.strip() | |
self.append(text) | |
def main(): | |
import sys | |
app = QtGui.QApplication(sys.argv) | |
win = MainWindow() | |
win.show() | |
sys.exit(app.exec_()) | |
if __name__ == '__main__': | |
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8"?> | |
<ui version="4.0"> | |
<class>MainWindow</class> | |
<widget class="QMainWindow" name="MainWindow"> | |
<property name="geometry"> | |
<rect> | |
<x>0</x> | |
<y>0</y> | |
<width>800</width> | |
<height>600</height> | |
</rect> | |
</property> | |
<property name="windowTitle"> | |
<string>MainWindow</string> | |
</property> | |
<widget class="QWidget" name="centralwidget"> | |
<layout class="QVBoxLayout" name="verticalLayout"> | |
<item> | |
<widget class="QTextEdit" name="textEdit"> | |
<property name="font"> | |
<font> | |
<pointsize>12</pointsize> | |
</font> | |
</property> | |
<property name="readOnly"> | |
<bool>true</bool> | |
</property> | |
</widget> | |
</item> | |
</layout> | |
</widget> | |
<widget class="QStatusBar" name="statusbar"/> | |
</widget> | |
<resources/> | |
<connections/> | |
</ui> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from time import sleep | |
from random import randrange, choice | |
from sys import stderr, stdout | |
def main(): | |
options = 2 * ['good'] + ['bad'] | |
with open('log.txt', 'w') as f: | |
for i in range(10): | |
sleep(randrange(1, 3)) | |
option = choice(options) | |
if option == 'good': | |
stdout.write('good\n') | |
stdout.flush() | |
else: | |
stderr.write('bad\n') | |
stderr.flush() | |
f.write(option + '\n') | |
f.flush() | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment