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).
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
window.show() # Call .show() on that window object, making the frame visible but the widget is not drawn.
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?