我正在将“ stdout”和“ stderr”传递到pyqt5 GUI中的嵌入式QTextBrowser小部件。一切正常,但是如果通过管道传输很长的输出(始终发生),则小部件的ScrollBar始终位于最后一个较高的位置,因此我无法实时看到新的输出。真烦人!一世 尝试了我可以在互联网上找到的所有内容,但仍然没有成功。 不会太难。...请睁开眼睛!
我正在使用的代码在这里(我认为它是在StackOverflow上找到的):
import sys
from PyQt4 import QtCore, QtGui
import logging
logger = logging.getLogger(__name__)
class QtHandler(logging.Handler):
def __init__(self):
logging.Handler.__init__(self)
def emit(self, record):
record = self.format(record)
XStream.stdout().write("{}\n".format(record))
handler = QtHandler()
handler.setFormatter(logging.Formatter("%(levelname)s: %(message)s"))
logger.addHandler(handler)
logger.setLevel(logging.DEBUG)
class XStream(QtCore.QObject):
_stdout = None
_stderr = None
messageWritten = QtCore.pyqtSignal(str)
def flush( self ):
pass
def fileno( self ):
return -1
def write( self, msg ):
if ( not self.signalsBlocked() ):
self.messageWritten.emit(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(QtGui.QDialog):
def __init__( self, parent = None ):
super(MyDialog, self).__init__(parent)
self._console = QtGui.QTextBrowser(self)
self._console.moveCursor(QtGui.QTextCursor.End)
layout = QtGui.QVBoxLayout()
layout.addWidget(self._console)
self.setLayout(layout)
XStream.stdout().messageWritten.connect(self._console.insertPlainText)
XStream.stderr().messageWritten.connect(self._console.insertPlainText)
self.pipe_output()
def pipe_output( self ):
logger.debug('debug message')
logger.info('info message')
logger.warning('warning message')
logger.error('error message')
#print('Old school hand made print message')
if ( __name__ == '__main__' ):
#app = None
# if ( not QtGui.QApplication.instance() ):
app = QtGui.QApplication([])
dlg = MyDialog()
dlg.show()
#if ( app ):
app.exec_()
这是打印屏幕
答案 0 :(得分:2)
我过去做过类似的事情,一个日志消息查看器,在添加新消息时滚动到底部。它基于QTextEdit
,但是由于QTextBrowser
还具有verticalScrolBar
方法,我相信您可以轻松地将其用于QTextBrower
。
class LogMessageViewer(QtWidgets.QTextEdit):
def __init__(self, parent=None):
super().__init__(parent=parent)
self.setReadOnly(True)
self.setLineWrapMode(QtWidgets.QTextEdit.NoWrap)
@pyqtSlot(str)
def appendLogMessage(self, msg):
horScrollBar = self.horizontalScrollBar()
verScrollBar = self.verticalScrollBar()
scrollIsAtEnd = verScrollBar.maximum() - verScrollBar.value() <= 10
self.append(msg)
if scrollIsAtEnd:
verScrollBar.setValue(verScrollBar.maximum()) # Scrolls to the bottom
horScrollBar.setValue(0) # scroll to the left
请注意,只有当当前滚动位置已经在底部的10像素之内时,它才会自动滚动。这使您可以向上滚动并查看较早的文本,而不会被新的输出中断。只需滚动回到底部即可再次获得实时更新。
答案 1 :(得分:0)
多亏了titusjan和eyllanesc,我才能够得到我想要的东西!
有效的代码是:
import sys
from PyQt5 import QtCore, QtGui
import logging
logger = logging.getLogger(__name__)
class QtHandler(logging.Handler):
def __init__(self):
logging.Handler.__init__(self)
def emit(self, record):
record = self.format(record)
XStream.stdout().write("{}\n".format(record))
handler = QtHandler()
handler.setFormatter(logging.Formatter("%(levelname)s: %(message)s"))
logger.addHandler(handler)
logger.setLevel(logging.DEBUG)
class XStream(QtCore.QObject):
_stdout = None
_stderr = None
messageWritten = QtCore.pyqtSignal(str)
def flush( self ):
pass
def fileno( self ):
return -1
def write( self, msg ):
if ( not self.signalsBlocked() ):
self.messageWritten.emit(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 LogMessageViewer(QtGui.QTextBrowser):
def __init__(self, parent=None):
super(LogMessageViewer,self).__init__(parent)
self.setReadOnly(True)
#self.setLineWrapMode(QtGui.QTextEdit.NoWrap)
@QtCore.pyqtSlot(str)
def appendLogMessage(self, msg):
horScrollBar = self.horizontalScrollBar()
verScrollBar = self.verticalScrollBar()
scrollIsAtEnd = verScrollBar.maximum() - verScrollBar.value() <= 10
self.insertPlainText(msg)
if scrollIsAtEnd:
verScrollBar.setValue(verScrollBar.maximum()) # Scrolls to the bottom
horScrollBar.setValue(0) # scroll to the left
class MyDialog(QtGui.QDialog):
def __init__( self, parent = None ):
super(MyDialog, self).__init__(parent)
self._console = LogMessageViewer(self)
layout = QtGui.QVBoxLayout()
layout.addWidget(self._console)
self.setLayout(layout)
XStream.stdout().messageWritten.connect(self._console.appendLogMessage)
XStream.stderr().messageWritten.connect(self._console.appendLogMessage)
if ( __name__ == '__main__' ):
app = QtGui.QApplication([])
dlg = MyDialog()
dlg.show()
app.exec_()
并且: