PyQt6
PyQt6 provides Python bindings for the Qt cross-platform application development framework, allowing developers to create desktop applications with native look and feel. It is currently at version 6.11.0 and typically releases new versions in close alignment with the major and minor releases of the underlying Qt library.
Warnings
- breaking Enum values (like `Qt.AlignRight`) must now be accessed via explicit enum types (e.g., `Qt.AlignmentFlag.AlignRight`, `Qt.ItemFlag.Selectable`). Direct access via `Qt.` is deprecated or removed in PyQt6.
- breaking The `QApplication` instance must be a singleton. Attempting to create a second `QApplication` will raise a `RuntimeError` (`RuntimeError: Application.instance() must be called before QApplication constructor`).
- gotcha Performing long-running or blocking operations directly on the main GUI thread will freeze your application's user interface, making it unresponsive.
- gotcha PyQt6 (Qt) objects have a parent-child ownership model. If an object has a parent, it is automatically deleted when its parent is deleted. Misunderstanding this can lead to memory leaks (orphaned objects without parents) or premature object deletion.
Install
-
pip install PyQt6
Imports
- QApplication
from PyQt6.QtWidgets import QApplication
- QWidget
from PyQt6.QtWidgets import QWidget
- QLabel
from PyQt6.QtWidgets import QLabel
- QVBoxLayout
from PyQt6.QtWidgets import QVBoxLayout
- Qt
from PyQt6.QtCore import Qt
Quickstart
import sys
from PyQt6.QtWidgets import QApplication, QWidget, QLabel, QVBoxLayout
from PyQt6.QtCore import Qt
class MyWindow(QWidget):
def __init__(self):
super().__init__()
self.setWindowTitle("My PyQt6 App")
self.setGeometry(100, 100, 400, 200)
layout = QVBoxLayout()
label = QLabel("Hello, PyQt6!", self)
label.setAlignment(Qt.AlignmentFlag.AlignCenter)
layout.addWidget(label)
self.setLayout(layout)
if __name__ == "__main__":
app = QApplication(sys.argv)
window = MyWindow()
window.show()
sys.exit(app.exec())