PyQt5 QWebEngineView如何实现与JavaScript高效交互?

2026-05-16 17:351阅读0评论SEO问题
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计655个文字,预计阅读时间需要3分钟。

PyQt5 QWebEngineView如何实现与JavaScript高效交互?

准备工作 + 开发环境 + Python 3.8.1 + Windows 10 + 安装依赖 + pip install PyQt5 pip install PyQtWebEngine + Python端 + 1. 使用QWebChannel的registerObject方法(JsBridge名,JsBridge对象)注册回调对象JsBridge;在JavaScript中调用时使用JsBridge。

准备工作

开发环境

PyQt5 QWebEngineView如何实现与JavaScript高效交互?

  • Python 3.8.1
  • Windows 10

安装依赖

pip install PyQt5 pip install PyQtWebEngine

Python端

1.使用QWebChannel的registerObject("JsBridge名","JsBridge")方法注册回调

  • JsBridge名:在JavaScript中调用时使用的对象名称
  • JsBridge:被JavaScript调用的Python对象

2.JsBridge 对象

  • 入参

@QtCore.pyqtSlot(str) def log(self, message): print(message)

  • 出参

@QtCore.pyqtSlot(result=str) def getName(self): return "hello"

  • 出入参

@QtCore.pyqtSlot(str, result=str) def test(self, message): print(message) return "ok"

JavaScript端

在Html的<head>中添加

<script src='qrc:///qtwebchannel/qwebchannel.js'></script>

调用

new QWebChannel(qt.webChannelTransport, function(channel) { channel.objects.pythonBridge.test("hello",function(arg) { console.log("Python message: " + arg); alert(arg); }); });

调试(Chrome DevTools)

  1. 配置环境变量:QTWEBENGINE_REMOTE_DEBUGGING = port
  2. 使用Chromium内核的浏览器打开地址:127.0.0.1:port
  3. 使用PyCharm中可以在运行配置(Run/Debug Configurations)中的Environment variables中添加环境变量,用;号分隔,然后可以直接运行。

Demo

Python

1.JsBridge

from PyQt5 import QtCore class JsBridge(QtCore.QObject): @QtCore.pyqtSlot(str, result=str) def test(self, message): print(message) return "ok"

2.Application

from PyQt5 import QtCore from PyQt5 import QtWebEngineWidgets from PyQt5.QtCore import QDir from PyQt5.QtWebChannel import QWebChannel from PyQt5.QtWebEngineWidgets import QWebEngineView from PyQt5.QtWidgets import * class TestWindow(QMainWindow): def __init__(self): super().__init__() self.webView = QWebEngineView() self.webView.settings().setAttribute( QtWebEngineWidgets.QWebEngineSettings.JavascriptEnabled, True) channel = QWebChannel(self.webView.page()) self.webView.page().setWebChannel(channel) self.python_bridge = JsBridge(None) channel.registerObject("pythonBridge", self.python_bridge) layout = QVBoxLayout() layout.addWidget(self.webView) widget = QWidget() widget.setLayout(layout) self.setCentralWidget(widget) self.resize(900, 600) self.setWindowTitle('Test') qr = self.frameGeometry() cp = QDesktopWidget().availableGeometry().center() qr.moveCenter(cp) self.move(qr.topLeft()) self.show() html_path = QtCore.QUrl.fromLocalFile(QDir.currentPath() + "/assets/index.html") self.webView.load(html_path) if __name__ == '__main__': app = QApplication(sys.argv) m = TestWindow() sys.exit(app.exec_())

JavaScript

index.html

<!DOCTYPE html> <html lang="zh"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no"> <title>Test</title> <script src='qrc:///qtwebchannel/qwebchannel.js'></script> <script src="img.558idc.com/uploadfile/allimg/210402/2100091950-2.jpg" integrity="sha256-CSXorXvZcTkaix6Yvo6HppcZGetbYMGWSFlBw8HfCJo=" crossorigin="anonymous"></script> </head> <body> <button id="test">test</button> </body> <script> $(document).ready(function() { new QWebChannel(qt.webChannelTransport, function(channel) { $('#test').on('click', function() { channel.objects.pythonBridge.test("hello",function(arg) { console.log("Python message: " + arg); alert(arg); }); }); }); }); </script> </html>

本文作者: liaoheng
本文链接: liaoheng.me/2019/12/23/PyQt5-QWebEngineView与JavaScript交互/

以上就是如何让PyQt5中QWebEngineView与JavaScript交互的详细内容,更多关于QWebEngineView与JavaScript交互的资料请关注易盾网络其它相关文章!

本文共计655个文字,预计阅读时间需要3分钟。

PyQt5 QWebEngineView如何实现与JavaScript高效交互?

准备工作 + 开发环境 + Python 3.8.1 + Windows 10 + 安装依赖 + pip install PyQt5 pip install PyQtWebEngine + Python端 + 1. 使用QWebChannel的registerObject方法(JsBridge名,JsBridge对象)注册回调对象JsBridge;在JavaScript中调用时使用JsBridge。

准备工作

开发环境

PyQt5 QWebEngineView如何实现与JavaScript高效交互?

  • Python 3.8.1
  • Windows 10

安装依赖

pip install PyQt5 pip install PyQtWebEngine

Python端

1.使用QWebChannel的registerObject("JsBridge名","JsBridge")方法注册回调

  • JsBridge名:在JavaScript中调用时使用的对象名称
  • JsBridge:被JavaScript调用的Python对象

2.JsBridge 对象

  • 入参

@QtCore.pyqtSlot(str) def log(self, message): print(message)

  • 出参

@QtCore.pyqtSlot(result=str) def getName(self): return "hello"

  • 出入参

@QtCore.pyqtSlot(str, result=str) def test(self, message): print(message) return "ok"

JavaScript端

在Html的<head>中添加

<script src='qrc:///qtwebchannel/qwebchannel.js'></script>

调用

new QWebChannel(qt.webChannelTransport, function(channel) { channel.objects.pythonBridge.test("hello",function(arg) { console.log("Python message: " + arg); alert(arg); }); });

调试(Chrome DevTools)

  1. 配置环境变量:QTWEBENGINE_REMOTE_DEBUGGING = port
  2. 使用Chromium内核的浏览器打开地址:127.0.0.1:port
  3. 使用PyCharm中可以在运行配置(Run/Debug Configurations)中的Environment variables中添加环境变量,用;号分隔,然后可以直接运行。

Demo

Python

1.JsBridge

from PyQt5 import QtCore class JsBridge(QtCore.QObject): @QtCore.pyqtSlot(str, result=str) def test(self, message): print(message) return "ok"

2.Application

from PyQt5 import QtCore from PyQt5 import QtWebEngineWidgets from PyQt5.QtCore import QDir from PyQt5.QtWebChannel import QWebChannel from PyQt5.QtWebEngineWidgets import QWebEngineView from PyQt5.QtWidgets import * class TestWindow(QMainWindow): def __init__(self): super().__init__() self.webView = QWebEngineView() self.webView.settings().setAttribute( QtWebEngineWidgets.QWebEngineSettings.JavascriptEnabled, True) channel = QWebChannel(self.webView.page()) self.webView.page().setWebChannel(channel) self.python_bridge = JsBridge(None) channel.registerObject("pythonBridge", self.python_bridge) layout = QVBoxLayout() layout.addWidget(self.webView) widget = QWidget() widget.setLayout(layout) self.setCentralWidget(widget) self.resize(900, 600) self.setWindowTitle('Test') qr = self.frameGeometry() cp = QDesktopWidget().availableGeometry().center() qr.moveCenter(cp) self.move(qr.topLeft()) self.show() html_path = QtCore.QUrl.fromLocalFile(QDir.currentPath() + "/assets/index.html") self.webView.load(html_path) if __name__ == '__main__': app = QApplication(sys.argv) m = TestWindow() sys.exit(app.exec_())

JavaScript

index.html

<!DOCTYPE html> <html lang="zh"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no"> <title>Test</title> <script src='qrc:///qtwebchannel/qwebchannel.js'></script> <script src="img.558idc.com/uploadfile/allimg/210402/2100091950-2.jpg" integrity="sha256-CSXorXvZcTkaix6Yvo6HppcZGetbYMGWSFlBw8HfCJo=" crossorigin="anonymous"></script> </head> <body> <button id="test">test</button> </body> <script> $(document).ready(function() { new QWebChannel(qt.webChannelTransport, function(channel) { $('#test').on('click', function() { channel.objects.pythonBridge.test("hello",function(arg) { console.log("Python message: " + arg); alert(arg); }); }); }); }); </script> </html>

本文作者: liaoheng
本文链接: liaoheng.me/2019/12/23/PyQt5-QWebEngineView与JavaScript交互/

以上就是如何让PyQt5中QWebEngineView与JavaScript交互的详细内容,更多关于QWebEngineView与JavaScript交互的资料请关注易盾网络其它相关文章!