PyQt JS调用Py

原创
2017/04/26 18:14
阅读数 1.3K

JS 调用 PyQt

HTML

<!-- js页面引入-->
<script src="qwebchannel.js"></script>
<script>
        window.onload = function() {
            new QWebChannel(qt.webChannelTransport, function(channel) {
                window.pyjs = channel.objects.pyjs;
                //py call js
				pyjs.result.connect(function(message){
					alert("connectCallResult:"+message);
				})

				//js call py
				var callResult = pyjs.myHello("loowj",function(returnValue){
					alert(returnValue);
				});
            });
        }

        function show(){
			alert("show");
			return "show";
		}
</script>

 PY

from PyQt5.QtCore import QObject, pyqtSlot
from PyQt5.QtWebChannel import QWebChannel
from PyQt5.QtWebEngineWidgets import QWebEngineView

#1
class CallHandler(QObject):
    result = pyqtSignal(int)

    @pyqtSlot(str,result=int)
    def myHello(self,sss):
        print("js call py :" + sss)
        self.result.emit(8888)
        return 123

#2
view = QWebEngineView()
channel = QWebChannel()
handler = CallHandler()
channel.registerObject('pyjs', handler)
view.page().setWebChannel(channel)
view.loadFinished.connect(self._loadFinish)

#3
    def _loadFinish(self,*args,**kwargs):
        view.page().runJavaScript("window.show()")
        print("load finish")

 

展开阅读全文
打赏
0
0 收藏
分享
加载中
更多评论
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部