我想从PyQt脚本中捕获stderr输出并将其显示在QMessageBox中。我找到了这些帖子并尝试了它们,但没有一个对我有效:
Display python console messages in QMessageBox
Redirecting stdout and stderr to a PyQt4 QTextEdit from a secondary thread
PYQT: How to capture output of python's interpreter and display it in QEditText?
我遇到了一些问题。首先,单个错误似乎是作为一系列单独的stderr消息发送的(至少在Python 3.4中)。这导致使用我找到的方法打开多个消息框,并且需要使用.show()
而不是.exec()
并将单独的消息累积到一个消息框中。
此外,如果发生第二个错误,即使第一个消息框已关闭,第一个错误中的文本仍保留在新消息框中。我尝试拨打.destroy()
并使用Qt.WA_DeleteOnClose
,但第一个消息框实际上并没有消失。所以我最终只是删除了持久性消息框中的文本。
找到一个干净的方式来同时关闭'关闭窗口'并且'好的'事件也进行了一些实验。
答案 0 :(得分:2)
我找到了一些似乎能完成这项工作的东西,就像我能做到的那样简单。如果有人有改进或可以解释我在这里工作的问题,请编辑代码。
这是一个示例,它启动一个简单的主窗口,其中包含一个用于创建stderr的按钮。将捕获错误消息并将其显示在消息框中,该消息框可以正确清除'确定'或关闭。该技术也可以与stdout一起使用。
from PyQt4 import QtGui
class StdErrHandler():
def __init__(self):
# To instantiate only one message box
self.err_box = None
def write(self, std_msg):
# All that stderr or stdout require is a class with a 'write' method.
if self.err_box is None:
self.err_box = QtGui.QMessageBox()
# Both OK and window delete fire the 'finished' signal
self.err_box.finished.connect(self.clear)
# A single error is sent as a string of separate stderr .write() messages,
# so concatenate them.
self.err_box.setText(self.err_box.text() + std_msg)
# .show() is used here because .exec() or .exec_() create multiple
# MessageBoxes.
self.err_box.show()
def clear(self):
# QMessageBox doesn't seem to be actually destroyed when closed, just hidden.
# This is true even if destroy() is called or if the Qt.WA_DeleteOnClose
# attribute is set. Clear text for next time.
self.err_box.setText('')
class AppMainWindow(QtGui.QMainWindow):
"""
Main window with button to create an error
"""
def __init__(self, parent=None):
# initialization of the superclass
super(AppMainWindow, self).__init__(parent)
self.create_err = QtGui.QPushButton(self)
self.create_err.setText("Create Error")
self.create_err.clicked.connect(self.err_btn_clicked)
def err_btn_clicked(self):
# Deliberately create a stderr output
oopsie
if __name__ == "__main__":
import sys
app = QtGui.QApplication(sys.argv)
amw = AppMainWindow()
amw.show()
# Instantiate stderr handler class
std_err_handler = StdErrHandler()
# Connect stderr to the handler
sys.stderr = std_err_handler
sys.exit(app.exec_())
修改强> 正如three_pineapples指出的那样,这不是线程安全的。我发现当我在另一个应用程序中安装此消息时,如果在pydev调试器下运行应用程序时出现stderr,则消息框将挂起,这可能是由于此原因。
我使用线程和队列重写了它,但这也挂了。
然后我了解到PyQt信号和&插槽是线程安全的,所以我只用它重写了它。下面的版本是线程安全的(我认为)并作为独立脚本或在调试器下正确运行:
from PyQt4 import QtCore, QtGui
class StdErrHandler(QtCore.QObject):
'''
This class provides an alternate write() method for stderr messages.
Messages are sent by pyqtSignal to the pyqtSlot in the main window.
'''
err_msg = QtCore.pyqtSignal(str)
def __init__(self, parent=None):
QtCore.QObject.__init__(self)
def write(self, msg):
# stderr messages are sent to this method.
self.err_msg.emit(msg)
class AppMainWindow(QtGui.QMainWindow):
'''
Main window with button to create an error
'''
def __init__(self, parent=None):
# initialization of the superclass
super(AppMainWindow, self).__init__(parent)
# To avoid creating multiple error boxes
self.err_box = None
# Single button, connect to button handler
self.create_err = QtGui.QPushButton(self)
self.create_err.setText("Create Error")
self.create_err.clicked.connect(self.err_btn_clicked)
def err_btn_clicked(self):
# Deliberately create a stderr output
oopsie
def std_err_post(self, msg):
'''
This method receives stderr text strings as a pyqtSlot.
'''
if self.err_box is None:
self.err_box = QtGui.QMessageBox()
# Both OK and window delete fire the 'finished' signal
self.err_box.finished.connect(self.clear)
# A single error is sent as a string of separate stderr .write() messages,
# so concatenate them.
self.err_box.setText(self.err_box.text() + msg)
# .show() is used here because .exec() or .exec_() create multiple
# MessageBoxes.
self.err_box.show()
def clear(self):
# QMessageBox doesn't seem to be actually destroyed when closed, just hidden.
# This is true even if destroy() is called or if the Qt.WA_DeleteOnClose
# attribute is set. Clear text for next time.
self.err_box.setText('')
if __name__ == '__main__':
import sys
app = QtGui.QApplication(sys.argv)
amw = AppMainWindow()
amw.show()
# Create the stderr handler and point stderr to it
std_err_handler = StdErrHandler()
sys.stderr = std_err_handler
# Connect err_msg signal to message box method in main window
std_err_handler.err_msg.connect(amw.std_err_post)
sys.exit(app.exec_())