PyQt5
PyQt5 is a comprehensive set of Python bindings for the Qt cross-platform application toolkit, enabling developers to create graphical user interfaces (GUIs) for desktop and mobile systems. It leverages the robust C++ Qt framework, providing high-level APIs for UI development, multimedia, networking, databases, and more. The current stable version is 5.15.11, maintained by Riverbank Computing Ltd. While PyQt5 is mature and stable, its successor, PyQt6, is available, targeting Qt6.
Warnings
- breaking The `exec()` method on QApplication and QDialog instances was renamed to `exec_()` in PyQt5 (and earlier versions targeting Python 2) to avoid conflicts with Python's `exec` keyword. While `exec()` works in PyQt5 for Python 3, `exec_()` is the widely adopted and safer convention for PyQt5 applications. In PyQt6, it reverted to `exec()`.
- gotcha Development tools like Qt Designer (`designer.exe`), `pyuic5`, and `pyrcc5` are no longer included directly with the `PyQt5` wheel installation. They are provided in a separate package, `pyqt5-tools`.
- breaking Migration from PyQt4 to PyQt5 involves significant breaking changes, notably the removal of the 'old-style' `QObject.connect()` signal/slot syntax and the re-organization of classes from the `QtGui` module into `QtGui`, `QtWidgets`, and `QtPrintSupport` modules.
- gotcha With the release of PyQt6 (targeting Qt6), PyQt5 is considered a mature but older generation. While PyQt5 is still actively maintained, new projects are often encouraged to start with PyQt6 for modern features, better performance, and long-term support. There are differences in enum naming (fully qualified names in PyQt6), module organization (e.g., `QAction` moved), and High DPI scaling defaults.
- gotcha Official PyQt5 documentation often links to the C++ Qt documentation. This means users may need to infer Python equivalents from C++ signatures and concepts, which can be a learning curve for those unfamiliar with C++.
Install
-
pip install PyQt5
Imports
- QApplication
from PyQt5.QtWidgets import QApplication
- QWidget
from PyQt5.QtWidgets import QWidget
- QLabel
from PyQt5.QtWidgets import QLabel
- QPushButton
from PyQt5.QtWidgets import QPushButton
- QVBoxLayout
from PyQt5.QtWidgets import QVBoxLayout
- Qt
from PyQt5.QtCore import Qt
Quickstart
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QLabel, QVBoxLayout
from PyQt5.QtCore import Qt
class MyWindow(QWidget):
def __init__(self):
super().__init__()
self.setWindowTitle('PyQt5 Hello World')
self.setGeometry(100, 100, 400, 200) # x, y, width, height
layout = QVBoxLayout()
self.label = QLabel('Hello, PyQt5!', self)
self.label.setAlignment(Qt.AlignCenter)
self.label.setStyleSheet("font-size: 24px; color: blue;")
layout.addWidget(self.label)
self.setLayout(layout)
if __name__ == '__main__':
app = QApplication(sys.argv)
window = MyWindow()
window.show()
sys.exit(app.exec_())