PyQt6 WebEngine
PyQt6-WebEngine provides Python bindings for The Qt Company's Qt WebEngine framework, allowing developers to embed web content into their applications. It leverages Chromium, an open-source web browser project, to offer a fast and secure browsing experience. The library sits atop PyQt6, implementing the WebEngine functionality through three distinct modules. Releases typically occur roughly every six months, aligning with the upstream Qt WebEngine releases.
Warnings
- breaking The QtWebEngineWidgets module, which was part of the main PyQt5 installation, has been moved to a separate `pyqt6-webengine` package for PyQt6. Direct imports from `PyQt6` (e.g., `from PyQt6 import QtWebEngineWidgets`) will fail if `pyqt6-webengine` is not explicitly installed and imported from `PyQt6.QtWebEngineWidgets`.
- gotcha Initializing QtWebEngine components (like `QWebEngineView`) sometimes requires the `QApplication` instance to be created first. Attempting to import or use `QtWebEngineWidgets` before `QApplication` is set up can lead to `ImportError: DLL load failed` or other unexpected behavior, especially on Windows.
- gotcha Users have reported performance issues, including flickering, with `QWebEngineView` when displaying WebGL content, particularly on Windows with secondary screens. This appears to be an upstream issue within Qt WebEngine 6.x and was less prevalent in Qt 5.15.
- gotcha JavaScript errors within `QWebEnginePage` (accessible via `QWebEngineView.page()`) when dealing with web requests, especially authentication systems, can often be traced back to invalid or unrecognized HTTP request headers.
- breaking In Qt6 (and thus PyQt6), many short-form enums (e.g., `Qt.Checked`) have been removed. Only the long-form equivalents (e.g., `Qt.CheckState.Checked`) are available. This is a general PyQt6 change but affects any code interacting with Qt APIs, including WebEngine.
Install
-
pip install PyQt6 PyQt6-WebEngine
Imports
- QWebEngineView
from PyQt6.QtWebEngineWidgets import QWebEngineView
- QUrl
from PyQt6.QtCore import QUrl
- QApplication
from PyQt6.QtWidgets import QApplication
Quickstart
import sys
from PyQt6.QtWidgets import QApplication, QWidget, QVBoxLayout
from PyQt6.QtWebEngineWidgets import QWebEngineView
from PyQt6.QtCore import QUrl
app = QApplication(sys.argv)
window = QWidget()
window.setWindowTitle('Simple Web Browser')
window.setGeometry(100, 100, 800, 600)
layout = QVBoxLayout()
window.setLayout(layout)
view = QWebEngineView()
layout.addWidget(view)
# Load a URL
view.setUrl(QUrl('https://www.google.com/'))
window.show()
sys.exit(app.exec())