且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

python:使用 PyCharm 和 PyQt5 时,进程已完成,退出代码为 1

更新时间:2022-03-17 05:37:41

我也处理过同样的问题,答案是双重的:

I have dealt with the same problem, and the answer is twofold:

  1. 它崩溃的原因可能有多种.这可能是一个编程错误,调用了一个不存在的函数,传递了一个小部件而不是布局等.但是由于您没有得到有用的输出,您不知道去哪里寻找罪魁祸首.这是由以下原因引起的:
  2. PyQT 会引发和捕获异常,但不会传递它们.相反,它只是以 1 状态退出,以显示捕获了异常.

要捕获异常,您需要覆盖 sys 异常处理程序:

To catch the exceptions, you need to overwrite the sys exception handler:

# Back up the reference to the exceptionhook
sys._excepthook = sys.excepthook

def my_exception_hook(exctype, value, traceback):
    # Print the error and traceback
    print(exctype, value, traceback)
    # Call the normal Exception hook after
    sys._excepthook(exctype, value, traceback)
    sys.exit(1)

# Set the exception hook to our wrapping function
sys.excepthook = my_exception_hook

然后在您的执行代码中,将其包装在 try/catch 中.

Then in your execution code, wrap it in a try/catch.

try:
    sys.exit(app.exec_())
except:
    print("Exiting")