pyside2中打开的外部页面WebEngineView与python部分的交互
19年末在涉及到python中打开外部页面的应用时,花了很多时间在网上找可实现的方式,从pyqt5到pyside2,最后是从国外的网站找到了可用的demo,和大家分享一下.(python3以上才支持pyside2)
附上参考的网址:https://justcode.nimbco.com/PyInstaller-with-Qt5-WebEngineView-using-PySide2/
首先在python代码中
import os
from pathlib import Path
from PySide2.QtWidgets import QApplication
from PySide2.QtWebEngineWidgets import QWebEngineView, QWebEnginePage
from PySide2.QtWebChannel import QWebChannel
from PySide2.QtCore import QUrl, Slot, QObject, QUrl
data_dir = Path(os.path.abspath(os.path.dirname(__file__))) / 'data'
class Handler(QObject):
def __init__(self, *args, **kwargs):
super(Handler, self).__init__(*args, **kwargs)
#@Slot是关键,是js中调用的接口,经测试,只能传一个参数,传对象参数时需要把它专为json字符串
@Slot(str, result=str)
def sayHello(self, name):
return f"Hello from the other side, {name}"
class WebEnginePage(QWebEnginePage):
def __init__(self, *args, **kwargs):
super(WebEnginePage, self).__init__(*args, **kwargs)
6298




被折叠的 条评论
为什么被折叠?



