What happens after defining `app`, but before executing it?

Heads up! You've already completed this tutorial.

9buzadani6114 | 2021-03-05 19:07:10 UTC | #1

Hi! I've just read the first tutorial, and it was great, but the thing I don't understand is how these lines of code work?

python
app = QApplication(sys.argv)

window = MainWindow("Hello, World!")
window.show()

app.exec_()

(The only modification I made is that the MainWindow class now accepts a text argument that will be shown by label)

Now by the first line we defined an instance of QApplication, and then in the 4th line we told our program to show the window class, but show it on what?(, we haven't specified any arguments for .show()) Is it a built-in workflow in PyQt5 that recognises a QApplication instance and shows anything that comes after onto that QApp?


martin | 2021-03-10 14:48:13 UTC | #2

Hi @9buzadani6114 welcome to the forum!

This can be a little tricky to understand if you're new to Python classes or event-based programming. I've modified the code below, adding some comments and pauses to show what is happening on each line. The window is replaced with a QPushButton which should show some text (any widget without a parent is a window in Qt).

Create GUI Applications with Python & Qt5 by Martin Fitzpatrick — (PyQt5 Edition) The hands-on guide to making apps with Python — Over 10,000 copies sold!

More info Get the book

python
import time
from PyQt5.QtWidgets import QApplication, QPushButton

app = QApplication([])   # Create the QApplication object, set up the Qt event queue

print("Create Window object")
window = QPushButton("Hello, World!")  # Create a MainWindow object, store it in window
time.sleep(10)

print("Show")
window.show()  # Call .show() on that window object, making the frame visible but the widget is not drawn.
time.sleep(10)

print("Exec")
app.exec_()  # Start the Qt event loop, the button widget becomes visible. You can press the button.

The key bit to understand is that until you start the Qt event loop (bottom line) your UI won't do anything. When you create the MainWindow, the object is created -- along with any objects for widgets in the window but is not shown. When you call .show() the "window frame" will become visible but the widget itself will not be drawn, and interactions will not be handled.

Any changes to widgets and interactions you have with the window are put onto Qt's event queue. Qt runs an event loop which picks these events off the queue and does something with them -- for example, re-drawing a window when something changes. The event loop is started by app.exec_() so none of these things will happen until you reach that line.

Hope that clears it up for you?


Well done, you've finished this tutorial! Mark As Complete
[[ user.completed.length ]] completed [[ user.streak+1 ]] day streak

What happens after defining `app`, but before executing it? was written by Martin Fitzpatrick .

Martin Fitzpatrick has been developing Python/Qt apps for 8 years. Building desktop applications to make data-analysis tools more user-friendly, Python was the obvious choice. Starting with Tk, later moving to wxWidgets and finally adopting PyQt.