Should I use `app.exec()` or `app.exec_()` in my PyQt application?

后端 未结 2 1694
暖寄归人
暖寄归人 2020-12-29 01:48

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

clas         


        
相关标签:
2条回答
  • 2020-12-29 02:04

    That's because until Python 3, exec was a reserved keyword, so the PyQt devs added underscore to it. From Python 3 onwards, exec is no longer a reserved keyword (because it is a builtin function; same situation as print), so it made sense in PyQt5 to provide a version without an underscore to be consistent with C++ docs, but keep a version with underscore for backwards compatibility. So for PyQt5 with Python 3, the two exec functions are the same. For older PyQt, only exec_() is available.

    0 讨论(0)
  • 2020-12-29 02:17

    On the question of whether to prefer one over the other: using exec_ means you have one less thing to worry about if you ever decide to add support for PyQt4 and/or Python >= 2.6, and want to maintain a single code-base.

    0 讨论(0)
提交回复
热议问题