Learn Python PyQt

PyQt Hello World

PyQt5 is a framework for Python to make GUI applications. The module has over 620 classes and 6000 functions and methods. It is a cross-platform GUI toolkit which can run on all major operating systems including UNIX, Windows, Mac OS. PyQt5 is double licensed. Developers can choose between a GPL and a commercial license.

PyQt5 installation

PyQt5 installation via conda

conda install pyqt

Installing PyQt5 via pip

pip install pyqt5

Book: Create Desktop Apps with Python PyQt5

PyQt Hello World Example

A simple window

import sys

from PyQt5.QtWidgets import QApplication, QWidget

if __name__ == '__main__':
    app = QApplication(sys.argv)

    w = QWidget()
    w.resize(250, 150)
    w.move(300, 300)
    w.setWindowTitle('Simple')
    w.show()

    sys.exit(app.exec_())

Code Interpretation
QtWidgets component module is imported here.

import sys
from PyQt5.QtWidgets import QApplication, QWidget

Each PyQt5 application must create an application object. sys.argv is a list of command line arguments. python can be run in the shell. this parameter provides control over the script.

app = QApplication(sys.argv)

The QWidget control is a basic UI control that provides the basic application constructor. By default, the constructor is without a parent, and a constructor without a parent is called a window.

w = QWidget()

The resize() method can change the size of the control, which means that the window is 250px wide and 150px high.

w.resize(250, 150)

move() is a way to modify the position of the control. It places the control to the (300, 300) position of the screen coordinates. Note: The origin of the screen coordinate system is the top left corner of the screen.

w.setWindowTitle('Simple')

We added a title to this window, which is displayed in the title bar

w.show()

Finally, we are in the main loop of the application and the event processor starts working at this time. The main loop receives events from the window and passes them in to the dispatch to the application control. The main loop ends when the exit() method is called or when the main control is destroyed directly. sys.exit() method ensures that the main loop is safely exited. The external environment can tell the master how to end.

sys.exit(app.exec_())
python pyqt hello world

Window with buttons

import sys
from PyQt5.QtWidgets import QApplication, QWidget, QPushButton, QHBoxLayout

if __name__ == '__main__':    
    app = QApplication(sys.argv)
    w = QWidget()    
    layout = QHBoxLayout()
    btn = QPushButton("Hello World!")
    layout.addWidget(btn)
    w.setLayout(layout)
    w.resize(250, 150)
    w.move(300, 300)
    w.setWindowTitle('Simple')
    w.show()
    sys.exit(app.exec_())

Code Interpretation

QHBoxLayout class, add controls in order from left to right. A new horizontal layout here.

layout = QHBoxLayout()

Create a new button

btn = QPushButton("Hello World!")

Add the buttons to the layout and the layout will arrange itself

layout.addWidget(btn)

Set the layout of the master

w.setLayout(layout)
button widget in python pyqt window

Book: Create Desktop Apps with Python PyQt5