Getting Started with PyQt Programming
1import sys
2from PyQt5 import QtWidgets
3
4# Create QApplication object
5app = QtWidgets.QApplication(sys.argv)
6# Create and display the widget
7button = QtWidgets.QPushButton("Hello World!")
8button.show()
9# Start the main loop
10app.exec_()
Understanding the PyQt Main Loop
Every GUI program has a central execution loop known as the event loop. In PyQt, this is provided by the
'QApplication'object. The event loop continually polls for events such as user input, and it responds according to the respective handler.'QApplication(sys.argv)': To start a PyQt GUI application, a'QApplication'object must be created.sys.argvis responsible for passing command line arguments to the application.QPushButton("Hello World!"): Creates a button widget. Here, “Hello World!” is the text displayed on the button.button.show(): Displays the widget (in this case, the button) on the screen.app.exec_(): Starts the main event loop. This method keeps the GUI application running by continually processing events.
Creating a Simple Window
The example above demonstrates how to create the most basic GUI application using PyQt5.
When you run this program, a window containing a button with the text “Hello World!” will appear.
This window remains visible until the user clicks the close button on the window.