Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 757
Northern Avatar asked Oct 31 '22 17:10

Northern


1 Answers

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.

like image 160
Kitsune Meyoko Avatar answered Nov 16 '22 10:11

Kitsune Meyoko