我的一些使用 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()
As b
不知道在TestForCrash
函数时,Qt 窗口刚刚退出,但控制台中没有任何内容。我想知道它们是否是一种强制控制台自动打印一些正在发生的事情的线索的方法。
现在我正在使用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
?
正如 @two_pineapples 提到的,当我在“真实”Windows 终端(使用 c:\anaconda3\python.exe)中执行脚本时出现错误,但在 PyCharm 控制台中(当我运行脚本时)却没有出现错误。那么他们是直接在 Pycharm 中强制错误日志的方法吗?也许这是我还没有找到的选项?