为什么 python 在 PyCharm 中的控制台在使用 pyqt 时不显示任何错误消息?
Why python console in PyCharm doesn't show any error message when pyqt is used?
我的一些使用 pyqt5 的代码遇到了一些问题。
当我的 Qt 类 出现问题时,控制台不会记录有关崩溃发生原因的任何信息。
例如使用此代码:
rom PyQt5.QtGui import *
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
import sys
class SurfViewer(QMainWindow):
def __init__(self, parent=None):
super(SurfViewer, self).__init__()
self.parent = parent
self.centralWidget = QWidget()
self.color = self.centralWidget.palette().color(QPalette.Background)
self.setCentralWidget(self.centralWidget)
self.plotview = QGroupBox(" ")
self.layout_plotview = QVBoxLayout()
self.Button_Crash= QPushButton('Crash!')
self.layout_plotview.addWidget(self.Button_Crash)
self.centralWidget.setLayout(self.layout_plotview)
self.Button_Crash.clicked.connect(self.TestForCrash)
def TestForCrash(self,):
a=b
return
def main():
app = QApplication(sys.argv)
ex = SurfViewer(app)
ex.show()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
由于 b
在 TestForCrash
函数中未知,Qt window 刚刚退出但我在控制台中什么也没有。我想知道他们是否是一种强制控制台自动打印正在发生的事情的线索的方法。
目前我正在使用 try
except
来解决这个问题,但我不太喜欢这个想法:
from PyQt5.QtGui import *
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
import sys
class SurfViewer(QMainWindow):
def __init__(self, parent=None):
super(SurfViewer, self).__init__()
self.parent = parent
self.centralWidget = QWidget()
self.color = self.centralWidget.palette().color(QPalette.Background)
self.setCentralWidget(self.centralWidget)
self.plotview = QGroupBox(" ")
self.layout_plotview = QVBoxLayout()
self.Button_Crash= QPushButton('Crash!')
self.layout_plotview.addWidget(self.Button_Crash)
self.centralWidget.setLayout(self.layout_plotview)
self.Button_Crash.clicked.connect(self.TestForCrash)
def TestForCrash(self,):
try:
a=b
except BaseException as e:
msg = QMessageBox()
msg.setIcon(QMessageBox.Critical)
msg.setText(str(e))
msg.setStandardButtons(QMessageBox.Ok)
msg.exec_()
return
def main():
app = QApplication(sys.argv)
ex = SurfViewer(app)
ex.show()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
他们是另一种不使用 try
except
在控制台中记录一些信息的方法吗?
如@three_pineapples所述,我在 'real' windows 终端(使用 c:\anaconda3\python.exe)中执行脚本时出现错误,但是不在 PyCharm 控制台中(当我 运行 脚本时)。那么他们是一种直接在 Pycharm 中强制错误记录的方法吗?也许这是我还没有找到的选项?
您可以做的是重新定义异常挂钩sys.excepthook
。引用文档:
When an exception is raised and uncaught, the interpreter calls sys.excepthook with three arguments, the exception class, exception instance, and a traceback object. In an interactive session this happens just before control is returned to the prompt; in a Python program this happens just before the program exits. The handling of such top-level exceptions can be customized by assigning another three-argument function to sys.excepthook.
您的自定义函数可以显示,例如,QMessagebox。这是在以下示例中的函数 catch_exceptions()
中完成的。该函数还调用旧的异常挂钩(存储在 old_hook
中),以便除了消息框之外还遵循处理异常的正常路径。
import sys
from PyQt5 import QtWidgets
def catch_exceptions(t, val, tb):
QtWidgets.QMessageBox.critical(None,
"An exception was raised",
"Exception type: {}".format(t))
old_hook(t, val, tb)
old_hook = sys.excepthook
sys.excepthook = catch_exceptions
def main():
app = QtWidgets.QApplication(sys.argv)
raise RuntimeError
if __name__ == "__main__":
main()
我的一些使用 pyqt5 的代码遇到了一些问题。 当我的 Qt 类 出现问题时,控制台不会记录有关崩溃发生原因的任何信息。 例如使用此代码:
rom PyQt5.QtGui import *
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
import sys
class SurfViewer(QMainWindow):
def __init__(self, parent=None):
super(SurfViewer, self).__init__()
self.parent = parent
self.centralWidget = QWidget()
self.color = self.centralWidget.palette().color(QPalette.Background)
self.setCentralWidget(self.centralWidget)
self.plotview = QGroupBox(" ")
self.layout_plotview = QVBoxLayout()
self.Button_Crash= QPushButton('Crash!')
self.layout_plotview.addWidget(self.Button_Crash)
self.centralWidget.setLayout(self.layout_plotview)
self.Button_Crash.clicked.connect(self.TestForCrash)
def TestForCrash(self,):
a=b
return
def main():
app = QApplication(sys.argv)
ex = SurfViewer(app)
ex.show()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
由于 b
在 TestForCrash
函数中未知,Qt window 刚刚退出但我在控制台中什么也没有。我想知道他们是否是一种强制控制台自动打印正在发生的事情的线索的方法。
目前我正在使用 try
except
来解决这个问题,但我不太喜欢这个想法:
from PyQt5.QtGui import *
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
import sys
class SurfViewer(QMainWindow):
def __init__(self, parent=None):
super(SurfViewer, self).__init__()
self.parent = parent
self.centralWidget = QWidget()
self.color = self.centralWidget.palette().color(QPalette.Background)
self.setCentralWidget(self.centralWidget)
self.plotview = QGroupBox(" ")
self.layout_plotview = QVBoxLayout()
self.Button_Crash= QPushButton('Crash!')
self.layout_plotview.addWidget(self.Button_Crash)
self.centralWidget.setLayout(self.layout_plotview)
self.Button_Crash.clicked.connect(self.TestForCrash)
def TestForCrash(self,):
try:
a=b
except BaseException as e:
msg = QMessageBox()
msg.setIcon(QMessageBox.Critical)
msg.setText(str(e))
msg.setStandardButtons(QMessageBox.Ok)
msg.exec_()
return
def main():
app = QApplication(sys.argv)
ex = SurfViewer(app)
ex.show()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
他们是另一种不使用 try
except
在控制台中记录一些信息的方法吗?
如@three_pineapples所述,我在 'real' windows 终端(使用 c:\anaconda3\python.exe)中执行脚本时出现错误,但是不在 PyCharm 控制台中(当我 运行 脚本时)。那么他们是一种直接在 Pycharm 中强制错误记录的方法吗?也许这是我还没有找到的选项?
您可以做的是重新定义异常挂钩sys.excepthook
。引用文档:
When an exception is raised and uncaught, the interpreter calls sys.excepthook with three arguments, the exception class, exception instance, and a traceback object. In an interactive session this happens just before control is returned to the prompt; in a Python program this happens just before the program exits. The handling of such top-level exceptions can be customized by assigning another three-argument function to sys.excepthook.
您的自定义函数可以显示,例如,QMessagebox。这是在以下示例中的函数 catch_exceptions()
中完成的。该函数还调用旧的异常挂钩(存储在 old_hook
中),以便除了消息框之外还遵循处理异常的正常路径。
import sys
from PyQt5 import QtWidgets
def catch_exceptions(t, val, tb):
QtWidgets.QMessageBox.critical(None,
"An exception was raised",
"Exception type: {}".format(t))
old_hook(t, val, tb)
old_hook = sys.excepthook
sys.excepthook = catch_exceptions
def main():
app = QtWidgets.QApplication(sys.argv)
raise RuntimeError
if __name__ == "__main__":
main()