时间:2021-05-22
准备工作
开发环境
安装依赖
pip install PyQt5pip install PyQtWebEnginePython端
1.使用QWebChannel的registerObject("JsBridge名","JsBridge")方法注册回调
2.JsBridge 对象
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)
Demo
Python
1.JsBridge
from PyQt5 import QtCoreclass JsBridge(QtCore.QObject): @QtCore.pyqtSlot(str, result=str) def test(self, message): print(message) return "ok"2.Application
from PyQt5 import QtCorefrom PyQt5 import QtWebEngineWidgetsfrom PyQt5.QtCore import QDirfrom PyQt5.QtWebChannel import QWebChannelfrom PyQt5.QtWebEngineWidgets import QWebEngineViewfrom 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="https://code.jquery.com/jquery-3.4.1.min.js" 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
本文链接: https://liaoheng.me/2019/12/23/PyQt5-QWebEngineView与JavaScript交互/
以上就是如何让PyQt5中QWebEngineView与JavaScript交互的详细内容,更多关于QWebEngineView与JavaScript交互的资料请关注其它相关文章!
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
这几天研究了下PyQt5中QWebEngineView内嵌网页与Python的数据交互,今天把实例方法与代码发布出来供大家参数数据交互需要load进一个网页,这
PyQt5浏览器控件QWebEngineViewPyQt5使用QWebEngineView控件来展示HTML页面,对老版本的QWebView类不在进行维护,因为
PyCharm不识别PyQt5的问题如图所示,引用PyQt5的时候显示错误“ModuleNotFoundError:Nomodulenamed'pyqt5'”首
事情是这样的,我在python中安装了PyQt5后,想查看QtGui模块中的类QMainWindow有哪些方法,如何查看呢?解决方法:1、原来在安装PyQt5时
一:安装PyQt5pipinstallpyqt5二:PyQt5简单使用1:使用PyQt5创建一个简单窗口importsysfromPyQt5importQtWi