Search code examples
pythonpyqtpysidesys

Difference between sys.exit(app.exec_()) and app.exec_()


I am working with PySide and PyQt for GUI development. I have been using these codes to run a GUI application:

app = QApplication(sys.argv)
ex = MyWin()
ex.show()
sys.exit(app.exec_())

Accidentally I found if I replace sys.exit(app.exec_()) with only app.exec_(), the program still works fine and it can exit correctly. So what is the difference between these two? Is there a reason I should use sys.exit(app.exec_())?


Solution

  • As I read the Python documentation, argument arg can be an integer giving the exit status. So return of app.exec_() can tell code exit status. As the documentation for QCoreApplication.exit (int returnCode = 0) says,

    By convention, a returnCode of 0 means success, and any non-zero value indicates an error.

    So the reason is to tell Python the code exit status from PyQt. If you avoid it, the program will close immediately.