PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Tuesday, July 12, 2022

[FIXED] Why python console in PyCharm doesn't show any error message when pyqt is used?

 July 12, 2022     console, message, pycharm, pyqt, python     No comments   

Issue

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?


Solution

What you can do is redefine the exception hook sys.excepthook. To quote from the documentation:

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.

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 in old_hook) so that the normal path of handling exceptions is followed in addition to the message box.

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()


Answered By - Schmuddi
Answer Checked By - Cary Denson (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home
View mobile version

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing