PySide6
PySide6 is the official Python binding for the Qt cross-platform application and UI framework, providing access to the complete Qt 6.0+ framework. It enables Python developers to create robust and visually appealing graphical user interface (GUI) applications for desktop and other platforms. Currently at version 6.11.0, PySide6 is actively maintained by The Qt Company with releases often synchronized with the underlying Qt framework.
Warnings
- breaking When migrating from PySide2 to PySide6, the primary change required is updating import statements from `PySide2.*` to `PySide6.*`.
- breaking The `QAction` class, used for creating toolbars and menus, has been moved from `PySide6.QtWidgets` to `PySide6.QtGui` in Qt6.
- breaking High DPI (dots per inch) scaling is enabled by default in Qt6. The previously used attributes like `Qt.AA_EnableHighDpiScaling` and `Qt.AA_UseHighDpiPixmaps` are deprecated and have no effect.
- gotcha `QMouseEvent.pos()` and `QMouseEvent.globalPos()` now return a `QPointF` object (floating-point coordinates), unlike in Qt5 where they returned integer tuples.
- gotcha Functions named `exec_()` (e.g., `QApplication.exec_()`, `QDialog.exec_()`) have been renamed to `exec()` in PySide6 to avoid conflict with the `exec` keyword in Python 3.
- gotcha Qt Enums no longer necessarily inherit to the top-level `Qt` object. For example, `Qt.AlignCenter` might not be directly accessible and requires explicit import or a more specific path.
- gotcha PySide6 offers an experimental `snake_case` feature (via `from __feature__ import snake_case`) to align method naming with PEP8. However, this is an opt-in feature and can lead to inconsistent code if not applied uniformly across a project, especially with auto-generated UI code.
Install
-
pip install PySide6
Imports
- QApplication
from PySide6.QtWidgets import QApplication
- QMainWindow
from PySide6.QtWidgets import QMainWindow
- QWidget
from PySide6.QtWidgets import QWidget
- QLabel
from PySide6.QtWidgets import QLabel
- QPushButton
from PySide6.QtWidgets import QPushButton
- QVBoxLayout
from PySide6.QtWidgets import QVBoxLayout
- QAction
from PySide6.QtGui import QAction
- Qt (for enums like alignment)
from PySide6.QtCore import Qt
Quickstart
import sys
from PySide6.QtWidgets import QApplication, QMainWindow, QLabel, QVBoxLayout, QWidget, QPushButton
from PySide6.QtCore import Qt
class MyWindow(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle('PySide6 Hello World!')
self.setGeometry(100, 100, 400, 200)
layout = QVBoxLayout()
label = QLabel('Hello, PySide6!')
label.setAlignment(Qt.AlignCenter)
layout.addWidget(label)
button = QPushButton('Click Me!')
button.clicked.connect(self.on_button_click)
layout.addWidget(button)
container = QWidget()
container.setLayout(layout)
self.setCentralWidget(container)
def on_button_click(self):
print('Button clicked!')
self.centralWidget().findChild(QLabel).setText('You clicked the button!')
if __name__ == '__main__':
app = QApplication(sys.argv)
window = MyWindow()
window.show()
sys.exit(app.exec())