这几天研究了下PyQt5中QWebEngineView内嵌网页与Python的数据交互,今天把实例方法与代码发布出来供大家参数
数据交互需要load进一个网页,这里我选择load进一个本地html网页:JSTest.html。
同时,QWebEngineView与外面的交互还需要Qt官方提供的一个js文件:qwebchannel.js,这个文件可以在网上下载。
JSTest.html和qwebchannel.js两个文件放在同一个目录下,我这边都是放在Python工程目录下。
qwebchannel.js:
/******************************************************************************** Copyright (C) 2016 The Qt Company Ltd.** Contact: http://pleteAndReturnName(){ var fname = document.getElementById('fname').value; var lname = document.getElementById('lname').value; var fullname = fname +' '+ lname; document.getElementById('fullname').value = fullname; document.getElementById('submit-btn').style.display = 'block'; return fullname; } document.addEventListener("DOMContentLoaded",function(){ new QWebChannel(qt.webChannelTransport, function (channel) { //alert('111 chanel='+channel) window.bridge = channel.objects.bridge; alert('bridge='+bridge.strValue +'\n从pyqt传来的参数='+window.bridge.strValue); //alert('111 chanel='+ channel.objects.strValue) }) } ) function onShowMsgBox() { if(window.bridge != null){ var fname = document.getElementById('fname').value; window.bridge.strValue = fname; //调用对象 } } </script> </head> <body> <form> <label id = "name">User Name:</label> <input type = "text" name = "fname" id = "fname"></input> <br /> <input type = "button" value="传递参数到pyqt" οnclick="onShowMsgBox()"></input> <br /> <input type = "reset" value="重置"></input> </form> </body> </html>实现效果
本文详细介绍了PyQt5中使用QWebEngineView控件内嵌网页与Python的数据交互的方法与实例,更多关于这方面的知识请查看下面的相关链接