开发一个系统,需要将系统的输出显示在PyQt中,可以直接截获sys.stdout,也可以使用信号和槽来实现。
直接显示:
"""
Example usage:
import sys
sys.stdout = OutLog( edit, sys.stdout)
sys.stderr = OutLog( edit, sys.stderr, QtGui.QColor(255,0,0) )
"""
class OutLog:
def __init__(self, edit, out=None, color=None):
"""(edit, out=None, color=None) -> can write stdout, stderr to a
QTextEdit.
edit = QTextEdit
out = alternate stream ( can be the original sys.stdout )
color = alternate color (i.e. color stderr a different color)
"""
self.edit = edit
self.out = None
self.color = color
def write(self, m):
global aSigalOutLog
if self.color:
tc = self.edit.textColor()
self.edit.setTextColor(self.color)
self.edit.moveCursor(QtGui.QTextCursor.End)
self.edit.insertPlainText(m)
if self.color:
self.edit.setTextColor(tc)
if self.out:
self.out.write(m)
Example usage:
import sys
sys.stdout = OutLog( edit, sys.stdout)
sys.stderr = OutLog( edit, sys.stderr, QtGui.QColor(255,0,0) )
"""
class OutLog:
def __init__(self, edit, out=None, color=None):
"""(edit, out=None, color=None) -> can write stdout, stderr to a
QTextEdit.
edit = QTextEdit
out = alternate stream ( can be the original sys.stdout )
color = alternate color (i.e. color stderr a different color)
"""
self.edit = edit
self.out = None
self.color = color
def write(self, m):
global aSigalOutLog
if self.color:
tc = self.edit.textColor()
self.edit.setTextColor(self.color)
self.edit.moveCursor(QtGui.QTextCursor.End)
self.edit.insertPlainText(m)
if self.color:
self.edit.setTextColor(tc)
if self.out:
self.out.write(m)
这种方法在多线程处理时可能有点问题,下面可以使用信号和槽来实现:
#encoding=gb2312
import sys
from PyQt4 import QtCore, QtGui
from PyQt4.QtCore import *
from PyMailServerMain import Ui_MainWindow
#信用信号和槽来处理日志
class SigalOutLogSender(QObject):
def SendMsg(self, m):
self.emit(SIGNAL('DisplayLog(QString)'),m)
aSigalOutLog=SigalOutLogSender()
class OutLog:
def __init__(self):
pass
def write(self, m):
global aSigalOutLog
aSigalOutLog.SendMsg(u'%s' % m)
class StartQt4(QtGui.QMainWindow):
def __init__(self, parent=None):
QtGui.QWidget.__init__(self, parent)
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
QtCore.QObject.connect(self.ui.btnSendMail,QtCore.SIGNAL("clicked()"), self.ProcessSend, 1)
#捕捉系统输出
sys.stdout = OutLog()
sys.stderr = OutLog()
#使用信号和槽来处理日志
global aSigalOutLog
QtCore.QObject.connect(aSigalOutLog,QtCore.SIGNAL("DisplayLog(QString)"), self.DisplayLog, 1)
def ProcessSend(self):
#使用信号和槽来处理日志
global aSigalOutLog
print u'测试标准输出'
def DisplayLog(self,log):
self.ui.memLog.moveCursor(QtGui.QTextCursor.End)
self.ui.memLog.insertPlainText(log)
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
myapp = StartQt4()
myapp.show()
sys.exit(app.exec_())
import sys
from PyQt4 import QtCore, QtGui
from PyQt4.QtCore import *
from PyMailServerMain import Ui_MainWindow
#信用信号和槽来处理日志
class SigalOutLogSender(QObject):
def SendMsg(self, m):
self.emit(SIGNAL('DisplayLog(QString)'),m)
aSigalOutLog=SigalOutLogSender()
class OutLog:
def __init__(self):
pass
def write(self, m):
global aSigalOutLog
aSigalOutLog.SendMsg(u'%s' % m)
class StartQt4(QtGui.QMainWindow):
def __init__(self, parent=None):
QtGui.QWidget.__init__(self, parent)
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
QtCore.QObject.connect(self.ui.btnSendMail,QtCore.SIGNAL("clicked()"), self.ProcessSend, 1)
#捕捉系统输出
sys.stdout = OutLog()
sys.stderr = OutLog()
#使用信号和槽来处理日志
global aSigalOutLog
QtCore.QObject.connect(aSigalOutLog,QtCore.SIGNAL("DisplayLog(QString)"), self.DisplayLog, 1)
def ProcessSend(self):
#使用信号和槽来处理日志
global aSigalOutLog
print u'测试标准输出'
def DisplayLog(self,log):
self.ui.memLog.moveCursor(QtGui.QTextCursor.End)
self.ui.memLog.insertPlainText(log)
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
myapp = StartQt4()
myapp.show()
sys.exit(app.exec_())