描述:我编写了一个自定义日志处理程序,用于捕获日志事件并将它们写入 QTextBrowser 对象(工作示例代码如下所示)。
问题:按下按钮调用someProcess()
。这会将两个字符串写入logger
对象。但是,字符串仅在someProcess()
返回后出现。
问题:如何让记录的字符串立即/实时显示在 QTextBrowser 对象中?(即一旦logger
调用输出方法)
from PyQt4 import QtCore, QtGui
import sys
import time
import logging
logger = logging.getLogger(__name__)
class ConsoleWindowLogHandler(logging.Handler):
def __init__(self, textBox):
super(ConsoleWindowLogHandler, self).__init__()
self.textBox = textBox
def emit(self, logRecord):
self.textBox.append(str(logRecord.getMessage()))
def someProcess():
logger.error("line1")
time.sleep(5)
logger.error("line2")
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
window = QtGui.QWidget()
textBox = QtGui.QTextBrowser()
button = QtGui.QPushButton()
button.clicked.connect(someProcess)
vertLayout = QtGui.QVBoxLayout()
vertLayout.addWidget(textBox)
vertLayout.addWidget(button)
window.setLayout(vertLayout)
window.show()
consoleHandler = ConsoleWindowLogHandler(textBox)
logger.addHandler(consoleHandler)
sys.exit(app.exec_())
编辑:感谢@abarnert 的回答,我设法使用QThread 编写了这段工作代码。QThread
为了someProcess
在后台线程中运行某些功能,我进行了子类化。对于信号,我不得不求助于旧式 Signal and Slots(我不确定如何在新式中做到这一点)。我创建了一个虚拟 QObject 以便能够从日志处理程序发出信号。
from PyQt4 import QtCore, QtGui
import sys
import time
import logging
logger = logging.getLogger(__name__)
#------------------------------------------------------------------------------
class ConsoleWindowLogHandler(logging.Handler):
def __init__(self, sigEmitter):
super(ConsoleWindowLogHandler, self).__init__()
self.sigEmitter = sigEmitter
def emit(self, logRecord):
message = str(logRecord.getMessage())
self.sigEmitter.emit(QtCore.SIGNAL("logMsg(QString)"), message)
#------------------------------------------------------------------------------
class Window(QtGui.QWidget):
def __init__(self):
super(Window, self).__init__()
# Layout
textBox = QtGui.QTextBrowser()
self.button = QtGui.QPushButton()
vertLayout = QtGui.QVBoxLayout()
vertLayout.addWidget(textBox)
vertLayout.addWidget(self.button)
self.setLayout(vertLayout)
# Connect button
self.button.clicked.connect(self.buttonPressed)
# Thread
self.bee = Worker(self.someProcess, ())
self.bee.finished.connect(self.restoreUi)
self.bee.terminated.connect(self.restoreUi)
# Console handler
dummyEmitter = QtCore.QObject()
self.connect(dummyEmitter, QtCore.SIGNAL("logMsg(QString)"),
textBox.append)
consoleHandler = ConsoleWindowLogHandler(dummyEmitter)
logger.addHandler(consoleHandler)
def buttonPressed(self):
self.button.setEnabled(False)
self.bee.start()
def someProcess(self):
logger.error("starting")
for i in xrange(10):
logger.error("line%d" % i)
time.sleep(2)
def restoreUi(self):
self.button.setEnabled(True)
#------------------------------------------------------------------------------
class Worker(QtCore.QThread):
def __init__(self, func, args):
super(Worker, self).__init__()
self.func = func
self.args = args
def run(self):
self.func(*self.args)
#------------------------------------------------------------------------------
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
window = Window()
window.show()
sys.exit(app.exec_())