This question already has an answer here:
I'm facing some issue with some of my code which use pyqt5. When something go wrong in my Qt classes, the console doesn't log any information about why the crashes happened. for instance with this code:
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
is not known in TestForCrash
function, the Qt window just quits but I've got nothing in the console. I'm wondering if their is a way to force the console to automatically print some clue of what is going on.
For now I'm using a try
except
to go around the issue but I don't like this idea much:
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()
Is their another way to log some info in the console without using a try
except
?
As mention by @three_pineapples, I've got errors when I execute the script in 'real' windows terminal (with c:\anaconda3\python.exe) but not in the PyCharm console (when I run the script). So is their a way to force error logs in Pycharm directly? maybe it is an option I didn't find yet?
What you can do is redefine the exception hook
sys.excepthook
. To quote from the documentation:Your custom function could display, for example, a QMessagebox. This is done in the function
catch_exceptions()
in the following example. That function also calls the old exception hook (stored inold_hook
) so that the normal path of handling exceptions is followed in addition to the message box.