I was trying to show both stdout and stderr in my PyQt (pyqtgraph) application. If I follow the Ferdinand Beyer's answer to this question, I am able to do so.
However, when I try to append a timestamp before the stdout by changing the text inside the write() function
new_text = '{}> {}'.format(time.strftime('%X'), old_text)
I get the timestamp attached before and after the stdout message. I am confused about this behavior and fail to figure out why it happens and how to prevent it.
UPDATE: my code directly:
from pyqtgraph.Qt import QtGui,QtCore
from pyqtgraph.dockarea import DockArea,Dock
import pyqtgraph as pg
import sys
import time
app = QtGui.QApplication([])
win = QtGui.QMainWindow()
win.resize(300,300)
area = DockArea()
win.setCentralWidget(area)
d1 = Dock("cmd output", size=(300,300))
area.addDock(d1)
w1 = pg.LayoutWidget()
gui_cmd_bw = QtGui.QTextBrowser()
w1.addWidget(gui_cmd_bw,0,0)
d1.addWidget(w1)
class EmittingStream(QtCore.QObject):
textWritten = QtCore.pyqtSignal(str)
def write(self, text):
self.textWritten.emit(str(text))
def normalOutputWritten(text):
"""Append text to the QTextEdit."""
# this works without problems.
#gui_cmd_bw.insertPlainText(text)
timestamp = '{}> '.format(time.strftime('%X'))
# this adds timestamp before and after the stdout
gui_cmd_bw.insertPlainText(timestamp+text)
# this produces similar result
#gui_cmd_bw.append(timestamp+text)
# cmd output to application browser
sys.stdout = EmittingStream(textWritten=normalOutputWritten)
sys.stderr = EmittingStream(textWritten=normalOutputWritten)
win.show()
if __name__ == '__main__':
if (sys.flags.interactive != 1) or not hasattr(QtCore, 'PYQT_VERSION'):
print('stdout text')
QtGui.QApplication.instance().exec_()
Final output looks like this:
14:15:16> stdout text
14:15:16>