Skip to content

Instantly share code, notes, and snippets.

@frogonwheels
Created April 9, 2010 06:25
Show Gist options
  • Save frogonwheels/360938 to your computer and use it in GitHub Desktop.
Save frogonwheels/360938 to your computer and use it in GitHub Desktop.
from PyQt4 import QtGui
from PyQt4 import QtCore
#from PyQt4 import *
import subprocess
import sys
class CommandTextView(QtGui.QWidget):
''' Nice TextView that reads the output of a command syncronously '''
def __init__(self, command):
'''command : the shell command to spawn'''
QtGui.QWidget.__init__(self) #, parent=None)
self._layout = QtGui.QVBoxLayout(self)
self._layout.setContentsMargins(3, 3, 3, 3)
self.output_text = QtGui.QTextEdit(self)
self.output_text.setAcceptDrops(False)
self.output_text.setTabChangesFocus(True)
self.output_text.setUndoRedoEnabled(False)
self.output_text.setReadOnly(True)
self.output_text.setAcceptRichText(False)
self._layout.addWidget(self.output_text)
self.command = command
def run(self):
''' Runs the process '''
#proc = subprocess.Popen(self.command, stdout = subprocess.PIPE) # Spawning
self.proc = QtCore.QProcess(self)
QtCore.QObject.connect(self.proc, SIGNAL("readyReadStdout()"), self.write_to_buffer())
self.proc.start(command)
def write_to_buffer(self):
strOut = self.proc.readAllStandardOutput()
cursor = self.output_text.textCursor()
cursor.movePosition(cursor.End)
text = self.output_text
cursor.insertText(strOut) # When running don't touch the TextView!!
cursor.movePosition(cursor.End)
text.setTextCursor(cursor)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment