@@ -11713,20 +11713,36 @@ def on_module_changed(self, text):
1171311713
1171411714def main ():
1171511715 app = QtWidgets .QApplication (sys .argv )
11716- window = Window ()
11717- window .show ()
1171811716
11719- def excepthook (type , value , tback ):
11720- lib .cancel_dialogs ()
11721- QtCore .QCoreApplication .instance ().processEvents ()
11722- message = "" .join (traceback .format_exception (type , value , tback ))
11723- errorbox = QtWidgets .QMessageBox .critical (
11724- window , "An error occured" , message
11717+ # Keep a reference so the excepthook closure can use it even before
11718+ # Window() returns. It is None only if construction itself raises.
11719+ window = None
11720+
11721+ def excepthook (exc_type , exc_value , exc_tb ):
11722+ message = "" .join (traceback .format_exception (exc_type , exc_value , exc_tb ))
11723+ try :
11724+ lib .cancel_dialogs ()
11725+ QtCore .QCoreApplication .instance ().processEvents ()
11726+ except Exception :
11727+ pass
11728+ # QMessageBox.critical already shows the dialog and returns the
11729+ # clicked button (an int) — do not call .exec_() on the return value.
11730+ QtWidgets .QMessageBox .critical (
11731+ window , "An error occurred" , message
1172511732 )
11726- errorbox .exec_ ()
11727- sys .__excepthook__ (type , value , tback )
11733+ sys .__excepthook__ (exc_type , exc_value , exc_tb )
1172811734
11735+ # Install before creating Window so construction errors are caught.
1172911736 sys .excepthook = excepthook
11737+
11738+ try :
11739+ window = Window ()
11740+ except Exception :
11741+ message = traceback .format_exc ()
11742+ QtWidgets .QMessageBox .critical (None , "Startup error" , message )
11743+ sys .exit (1 )
11744+
11745+ window .show ()
1173011746 sys .exit (app .exec_ ())
1173111747
1173211748
0 commit comments