Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 internal exec? I suspect because both exec's are executing something.
  • If not, can I use both interchangeably?
like image 422
socket Avatar asked Mar 24 '14 13:03

socket


People also ask

What does app exec _() do?

app. exec_() does not lock anything, it runs a GUI event loop that waits for user actions (events) and dispatches them to the right widget for handling.

Is PyQt good for desktop application?

Compared to Electron and JavaScript, PyQt5 might not be the popular tool to build desktop apps, but it's very effective. Web apps are very popular, but there are still times when only a desktop app can deliver a great user experience.

Can PyQt be used for Web application?

PyQt is designed for desktop application development e.g. using your desktops GUI elements, rather than in a browser. For web development with Python the two most common are Flask or Django. Flask is generally simpler/more lightweight but depends on what you need.


1 Answers

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.

like image 69
Oliver Avatar answered Oct 14 '22 10:10

Oliver