Good day! I am learning PyQt5 and trying to write a simple dictionary. The program starts, but when you close it, the message "Python program terminated" appears. enter image description here

The code is:

import datrie from PyQt5 import QtCore, QtGui from PyQt5.QtWidgets import QApplication, QWidget, QMessageBox from ui_dict_form import UiDictForm class DictWindow(QWidget, UiDictForm): def __init__(self, parent=None): QWidget.__init__(self, parent) self.setup_ui(self) self.setLayout(self.gridLayout) self.dict = datrie.Trie.load('file_dict.dict') def closeEvent(self, event): reply = QMessageBox.question(self, 'Message', "Вы действительно хотите выйти?", QMessageBox.Yes | QMessageBox.No, QMessageBox.No) if reply == QMessageBox.Yes: self.close() else: event.ignore() if __name__ == '__main__': import sys app = QApplication(sys.argv) window = DictWindow() window.show() sys.exit(app.exec_()) 

At the moment, I realized that such a message appears when there is such a line in the code: self.dict = datrie.Trie.load('file_dict.dict') , but if you remove / comment it out, then such a message does not appear. I use Windows 7 64bit, Python 3.5, PyQt5. Help to understand / get rid of this problem.

  • run the script to see the traceback. For example, from the console using python.exe - jfs
  • I do just that. But nothing appears - fat-complex
  • if the datrie is a C extension and there is an error at the C level, then you can enable the faulthandler module to see the trackback. - jfs

2 answers 2

To catch exceptions that occur in Qt slots, add the log_uncaught_exceptions function.

When there is an exception, you will see it and where it happened , which will help correct the error.

 from PyQt5.QtWidgets import QApplication, QMessageBox, QPushButton def log_uncaught_exceptions(ex_cls, ex, tb): text = '{}: {}:\n'.format(ex_cls.__name__, ex) import traceback text += ''.join(traceback.format_tb(tb)) print(text) QMessageBox.critical(None, 'Error', text) quit() import sys sys.excepthook = log_uncaught_exceptions if __name__ == '__main__': app = QApplication([]) button = QPushButton("DON'T PUSH ME!") button.resize(200, 200) button.clicked.connect(lambda: 1 / 0) button.show() app.exec() 

Result:

enter image description here

  • without sys.excepthook traceback would be printed in the console (the author of the question claims that nothing is visible in the console). At the C level, the application may fall. - jfs
  • the code in the answer without sys.excepthook will not print on the console, try it yourself, the author has the same problem, only the code is more - gil9red
  • The code (expectedly) prints an exception in the terminal on my machine. Are you sure you didn't use pythonw for launch? - jfs
  • Understood. Such an error as the author in question arose at startup from PyCharm. If you run outside of IDE, then when python.exe the error was displayed in the console (if not excepthook was intercepted), and the console did not appear in pythonw.exe, so the program silently fell - gil9red

The problem here is clearly not in pyqt, the problem is either in the datrie module, or (most likely) in the file file_dict.dict . Perhaps its contents do not match the desired format.