杰瑞科技汇

PyQtWebKit如何在Python中正确使用?

Of course! Let's dive deep into PyQtWebKit.

What is PyQtWebKit?

PyQtWebKit is a Python binding for the QtWebKit module, which is a web engine for rendering web content inside a Qt application. It was part of the original PyQt package.

In simple terms, it allows you to embed a fully functional web browser (like Google Chrome or Firefox) directly into your Python GUI application built with PyQt or PySide.


The Critical Warning: PyQtWebKit is Deprecated and Obsolete

This is the most important thing you need to know.

  • Why was it deprecated? QtWebKit was replaced by a new, much faster, more standards-compliant, and more secure web engine called QtWebEngine.
  • When was it deprecated? The last major release of QtWebKit was with Qt 5.5 in 2025. It has received no significant updates since.
  • The Problem: Modern websites (using JavaScript frameworks like React, Angular, Vue, etc.) will not work correctly, or at all, in QtWebKit. It lacks support for modern web APIs, has security vulnerabilities, and is incredibly slow compared to modern engines.

Conclusion: You should almost never use PyQtWebKit for any new project. It is a legacy technology.


When You Might Still Encounter PyQtWebKit

You will only find PyQtWebKit in a few specific scenarios:

  1. Maintaining Old Legacy Codebases: You might be asked to fix or extend an old application that was built before QtWebEngine existed.
  2. Accessing Old Web Content: If you need to scrape or interact with a very old, simple website that has not changed since the QtWebKit era and works perfectly with its rendering engine.

The Modern Alternative: PyQtWebEngine

For any new project, you must use PyQtWebEngine.

Feature PyQtWebKit (Legacy) PyQtWebEngine (Modern)
Engine WebKit (based on Apple's Safari/WebKit) Chromium (based on Google's Chrome)
Performance Slow, poor JavaScript performance Very fast, excellent JavaScript performance
Standards Poor HTML5, CSS3, and ECMAScript support Excellent, up-to-date support for all modern web standards
Security Multiple known vulnerabilities, no longer patched Actively maintained, sandboxed, much more secure
Features Basic web rendering Supports WebGL, WebRTC, WebAssembly, modern plugins
Python Binding from PyQt4.QtWebKit import QWebView (PyQt4) or from PyQt5.QtWebKitWidgets import QWebView (PyQt5) from PyQt5.QtWebEngineWidgets import QWebEngineView (PyQt5) or from PySide6.QtWebEngineWidgets import QWebEngineView (PySide6)

Practical Code Examples

Let's look at how to use both. This will highlight the differences.

PyQtWebKit (PyQt5) - The Old Way

This code will work on older systems but will fail on modern websites.

# This example requires PyQt5
# pip install PyQt5
import sys
from PyQt5.QtWidgets import QApplication, QMainWindow, QVBoxLayout, QWidget
from PyQt5.QtCore import QUrl
from PyQt5.QtWebKitWidgets import QWebView
class MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()
        self.setWindowTitle("PyQtWebKit Browser (Legacy)")
        self.setGeometry(100, 100, 1024, 768)
        # Create the WebView widget
        self.browser = QWebView()
        # Load a URL
        # Note: Google.com will likely look broken or not load at all.
        # A simple site like 'python.org' might work better.
        self.browser.setUrl(QUrl("https://www.python.org"))
        # Set the central widget
        self.setCentralWidget(self.browser)
if __name__ == '__main__':
    app = QApplication(sys.argv)
    window = MainWindow()
    window.show()
    sys.exit(app.exec_())

To run this, you need an older environment. On modern Linux (e.g., Ubuntu 22.04+), you might need to install it manually: sudo apt-get install python3-pyqt5.qtwebkit

PyQtWebEngine (PyQt5) - The Correct Way

This is how you should write a new application. It's the direct replacement.

# This example requires PyQt5
# pip install PyQt5
import sys
from PyQt5.QtWidgets import QApplication, QMainWindow, QVBoxLayout, QWidget
from PyQt5.QtCore import QUrl
from PyQt5.QtWebEngineWidgets import QWebEngineView
class MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()
        self.setWindowTitle("PyQtWebEngine Browser (Modern)")
        self.setGeometry(100, 100, 1024, 768)
        # Create the WebEngineView widget
        self.browser = QWebEngineView()
        # Load a URL
        self.browser.setUrl(QUrl("https://www.google.com"))
        # Set the central widget
        self.setCentralWidget(self.browser)
if __name__ == '__main__':
    app = QApplication(sys.argv)
    window = MainWindow()
    window.show()
    sys.exit(app.exec_())

Key differences:

  • Import: from PyQt5.QtWebEngineWidgets import QWebEngineView
  • Widget: self.browser = QWebEngineView() (not QWebView)
  • The rest of the API is very similar, making the migration path easier.

Common Use Cases for a Web Browser in a GUI

People often embed a browser in a desktop application for these reasons:

  1. Displaying Rich Content: Instead of building a complex UI with custom widgets, you can use HTML, CSS, and JavaScript for a rich, responsive interface.
  2. Web Scraping and Automation: Automate interactions with a website by controlling the browser from Python (e.g., filling out forms, clicking buttons, extracting data).
  3. Creating a "Chromium" Kiosk: Build a dedicated application that only displays one specific website, perfect for digital signage or information displays.
  4. Web App as a Desktop App (Electron-like): Wrap a web application in a native desktop window using a framework like PyQtWebEngine or Eel.

Summary

Topic Details
What is it? A Python binding for the old QtWebKit web engine to embed a browser in a PyQt/PySide app.
Status DEPRECATED and OBSOLETE. Do not use for new projects.
Modern Replacement PyQtWebEngine (or PySide6WebEngine). It's based on Chromium, fast, secure, and modern.
When to Use? Only for maintaining ancient legacy codebases or interacting with very old, simple websites.
How to Install? pip install PyQt5 (for WebEngine). For WebKit, it's often a system package (python3-pyqt5.qtwebkit).
Basic Code Use QWebEngineView instead of QWebView. The API is largely compatible.

Final Recommendation: Start all new projects with PyQtWebEngine. Forget PyQtWebKit exists unless you are specifically paid to maintain old software.

分享:
扫描分享到社交APP
上一篇
下一篇