I use Python 3 and PyQt5. Here's my test PyQt5 program, focus on the last 2 lines:
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
import sys
class window(QWidget):
def __init__(self,parent=None):
super().__init__(parent)
self.setWindowTitle('test')
self.resize(250,200)
app=QApplication(sys.argv)
w=window()
w.show()
sys.exit(app.exec())
#sys.exit(app.exec_())
I know exec
is a language keyword in Python. But code on Official PyQt5 Documentation (specifically the Object Destruction on Exit part). I see that example shows use of app.exec()
which confuses me.
When I tested it on my machine. I found there is no any visible difference from my end. Both with and without _
produces the same output in no time difference.
My question is:
- Is there anything wrong going when I use
app.exec()
? like clashing with Python's internalexec
? I suspect because bothexec
's are executing something. - If not, can I use both interchangeably?
sys.exit()
at all.app.exec_()
orapp.exec()
alone is enough and it works normally. They fixed some things in PyQt5 under the hood so that you don't need thatsys.exit()
anymore. If you want your code to run on PyQt4, then havesys.exit()
there. Also,app.exec_()
andapp.exec()
are interchangable, so you can use whichever you like. If you want your code to run on PyQt4 as well, then stick withapp.exec_()
. So, the bottom line is: for your code to work properly on PyQt5 and PyQt4, usesys.exit(app.exec_())
.sys.exit()
otherwiseapp.exit(1)
call doesn't lead to the corresponding exit status i.e., even on Python 3 and PyQt5, usesys.exit(app.exec())
.