PyQtWebEngine
PyQtWebEngine is a set of Python bindings for The Qt Company's Qt WebEngine framework, providing the ability to embed web content in PyQt5 applications. It integrates the Chromium-based web engine, allowing developers to display and interact with web pages. The bindings are implemented as three separate modules sitting on top of PyQt5. The current version is 5.15.7 and it is actively maintained.
Warnings
- breaking When migrating from PyQt5 to PyQt6, the QtWebEngine module is no longer bundled with the main PyQt installation. For PyQt6, you must install 'pyqt6-webengine' separately (pip install pyqt6-webengine) and the import path changes from 'PyQt5.QtWebEngineWidgets' to 'PyQt6.QtWebEngineWidgets'.
- gotcha Installing only 'PyQt5' will not include the 'QtWebEngineWidgets' module. 'PyQtWebEngine' is a separate package that must be installed explicitly.
- gotcha PyQtWebEngine is dual-licensed under the GNU GPL v3 and a commercial license. Unlike some other Qt modules, it is *not* available under the LGPL. This has significant implications for proprietary applications.
- gotcha Direct access to the Document Object Model (DOM) of a loaded web page is not provided by QWebEngineView. Interactions with the web content (e.g., executing JavaScript, getting values) are primarily achieved through `QWebEnginePage.runJavaScript()`.
Install
-
pip install PyQtWebEngine
Imports
- QWebEngineView
from PyQt5.QtWebEngineWidgets import QWebEngineView
- QWebEnginePage
from PyQt5.QtWebEngineWidgets import QWebEnginePage
- QUrl
from PyQt5.QtCore import QUrl
Quickstart
import sys
from PyQt5.QtWidgets import QApplication, QMainWindow
from PyQt5.QtWebEngineWidgets import QWebEngineView
from PyQt5.QtCore import QUrl
class Browser(QMainWindow):
def __init__(self):
super().__init__()
self.browser = QWebEngineView()
self.browser.setUrl(QUrl('http://www.google.com'))
self.setCentralWidget(self.browser)
self.showMaximized()
if __name__ == '__main__':
app = QApplication(sys.argv)
QApplication.setApplicationName("PyQtWebEngine Browser")
window = Browser()
sys.exit(app.exec_())