我正在面对一些使用pyqt5的代码的问题。 当我的Qt类出现问题时,控制台不会记录关于发生崩溃原因的任何信息。 例如,此代码:为什么PyCharm中的python控制台在使用pyqt时不显示任何错误消息?
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的窗口刚刚退出,但我已经没有什么在控制台中。我想知道如果他们是一种强制控制台自动打印正在发生的线索的方法。
现在我使用的是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所提到的,我在'真正的'windows终端(使用c:\ anaconda3 \ python.exe)中执行脚本时遇到了错误,但没有在PyCharm控制台(当我运行脚本时) 。那么他们是否直接在Pycharm中强制执行错误日志?也许这是一个我没有找到的选项?
您使用python.exe或python ** w **。exe来启动代码吗?前者应该向终端打印例外。 –
我使用PyCharm,我选择的解释器是c:\ anaconda3 \ python.exe。 但它不打印任何东西。 – ymmx
但是,你说得对,当我用c:\ anaconda3 \ python.exe运行Windows CMD脚本时,我在登录终端时出现错误。所以这个问题可能来自PyCharm? – ymmx