6

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

Northern
  • 2,338
  • 1
  • 17
  • 20

1 Answers1

2

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.

MarianD
  • 13,096
  • 12
  • 42
  • 54
Bandhit Suksiri
  • 3,390
  • 1
  • 18
  • 20
  • 8
    This is not 100% correct. It is not to tell *Python* about the exit code, but the *parent process* (probably a shell like bash, or `explorer.exe`). To a parent process, a return code of `0` means that everything was okay. Anything else is considered an abnormal program termination. This can be important if you batch your application. But in GUI applications that's probably not the case and not very important. Maybe, in Windows you'll get notified about the event viewer about that. But I'm not sure about that. I'm not a Windows person ;) – exhuma Sep 08 '14 at 11:25