在PyQt中重定向输出 [英] Redirecting Output in PyQt
问题描述
我有一个QApplication,有很多类和函数,它在控制台上显示了很多stdout。我想将这个stdout和stderr重定向到一个QTextBrowser(这也是QApplication的一部分)。有任何调整,这样做。
I have a QApplication, with a lot of classes and functions, which displays a lot of stdout on the console. I want to redirect this stdout and stderr to a QTextBrowser(this is also part of the QApplication). Is there any tweak to do this.
推荐答案
我创建了一个开源的PyQt自定义小部件库 - 其中一个是一个记录器类(XLoggerWidget)其中是一个完整的Python解释器(XConsoleEdit)。
I have made a library of open-source PyQt custom widgets - one of which is a logger class (XLoggerWidget) and another of which is a full Python interpreter (XConsoleEdit). It does what you're looking for.
您可以在这里取得: http://dev.projexsoftware.com/projects/projexui
您要查找的部分位于XConsoleEdit(projexui.widgets.xconsoleedit),但它的一般要点是:
The part you're looking for is in the XConsoleEdit (projexui.widgets.xconsoleedit), but the general gist of it is:
import logging
import sys
from PyQt4.QtCore import QObject,\
pyqtSignal
from PyQt4.QtGui import QDialog, \
QVBoxLayout, \
QPushButton, \
QTextBrowser,\
QApplication
logger = logging.getLogger(__name__)
class XStream(QObject):
_stdout = None
_stderr = None
messageWritten = pyqtSignal(str)
def flush( self ):
pass
def fileno( self ):
return -1
def write( self, msg ):
if ( not self.signalsBlocked() ):
self.messageWritten.emit(unicode(msg))
@staticmethod
def stdout():
if ( not XStream._stdout ):
XStream._stdout = XStream()
sys.stdout = XStream._stdout
return XStream._stdout
@staticmethod
def stderr():
if ( not XStream._stderr ):
XStream._stderr = XStream()
sys.stderr = XStream._stderr
return XStream._stderr
class MyDialog(QDialog):
def __init__( self, parent = None ):
super(MyDialog, self).__init__(parent)
# setup the ui
self._console = QTextBrowser(self)
self._button = QPushButton(self)
self._button.setText('Test Me')
# create the layout
layout = QVBoxLayout()
layout.addWidget(self._console)
layout.addWidget(self._button)
self.setLayout(layout)
# create connections
XStream.stdout().messageWritten.connect( self._console.insertPlainText )
XStream.stderr().messageWritten.connect( self._console.insertPlainText )
self._button.clicked.connect(self.test)
def test( self ):
# print some stuff
print 'testing'
print 'testing2'
# log some stuff
logger.debug('Testing debug')
logger.info('Testing info')
logger.warning('Testing warning')
logger.error('Testing error')
# error out something
print blah
if ( __name__ == '__main__' ):
logging.basicConfig()
app = None
if ( not QApplication.instance() ):
app = QApplication([])
dlg = MyDialog()
dlg.show()
if ( app ):
app.exec_()
这是XConsoleEdit中的内容的简化版本,但它的一般想法,如果你不想下载代码,仍应该为你打算做什么。
This is a simplified version of whats in the XConsoleEdit, but its the general idea and should still work for what you're going for if you don't want to download the code.
在这个例子中,你会注意到,只有打印和错误日志被路由到编辑。如果你想将Python日志系统连接到编辑,你需要一些更复杂的东西,你定义一个logging.Handler并链接到你的小部件。
In this example tho, you'll notice that only the print and error logs are routed through to the edit. If you want to connect the Python logging system to the edit, you'll need something a little more complex where you define a logging.Handler and link it to your widget.
该代码将在projexui.widgets.xloggerwidget中找到
That code would be found in projexui.widgets.xloggerwidget
它有点长,更复杂,所以我不会在这里加载它...但是如果您对此有任何疑问,请告诉我们。
Its a bit longer and more complex, so I'm not going to load it up here...but if you have any questions about it, let me know.
这篇关于在PyQt中重定向输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!