Created
April 9, 2010 06:25
-
-
Save frogonwheels/360938 to your computer and use it in GitHub Desktop.
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 | |
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