QtPy

2.4.3 · active · verified Thu Apr 09

QtPy provides an abstraction layer on top of the various Qt bindings (PyQt5, PyQt6, PySide2, and PySide6), allowing developers to write Python GUI applications that can run with any of these backends without modifying code. It automatically detects and uses the available binding, or can be configured to prefer a specific one. The current version is 2.4.3, with a regular release cadence addressing compatibility and feature enhancements.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates a basic QtPy application. It initializes a QApplication, creates a QWidget with a QLabel and QPushButton, sets up a simple layout, and connects a slot to the button's click signal. It highlights how to import modules from `qtpy` and also shows an optional environment variable `QT_API` that can be set to force a specific backend if multiple are installed.

import sys
import os
from qtpy import QtWidgets, QtCore

# Optional: force a specific Qt API (e.g., 'PyQt5', 'PyQt6', 'PySide2', 'PySide6')
# os.environ['QT_API'] = 'PyQt5'

# Create the application object
app = QtWidgets.QApplication(sys.argv)

# Create a simple widget
widget = QtWidgets.QWidget()
widget.setWindowTitle('Hello QtPy!')
widget.setGeometry(300, 300, 300, 150)

layout = QtWidgets.QVBoxLayout()
label = QtWidgets.QLabel('Welcome to QtPy!')
button = QtWidgets.QPushButton('Click Me')

layout.addWidget(label)
layout.addWidget(button)
widget.setLayout(layout)

def on_button_click():
    label.setText('Button clicked from ' + os.environ.get('QT_API', 'auto-detected') + '!')

button.clicked.connect(on_button_click)

# Show the widget
widget.show()

# Start the event loop
sys.exit(app.exec_())

view raw JSON →