Why doesn't the python console in PyCharm display an error message when using pyqt?

I ran into some problems with some of my code that use pyqt5. When something goes wrong in my Qt classes, the console does not log information about the causes of the failures. for example using 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()

Since it is bnot known in the function TestForCrash, the Qt window just terminates, but I have nothing on the console. I am wondering if their way is to force the console to automatically print out some information about what is happening.

I am currently using try exceptto work around the problem, but I don't like this idea:

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 this another way to log some information into the console without using try except?

@three_pineapples, , script "" Windows ( c:\anaconda3\python.exe), PyCharm ( script). Pycharm ? , , ?

+1
1

, sys.excepthook. :

, sys.excepthook , , traceback. ; Python . 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()
+2

Source: https://habr.com/ru/post/1675738/


All Articles