1 Star 0 Fork 308

GSAM / PyQt

forked from PyQt5 / PyQt 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
JsSignals.py 2.54 KB
一键复制 编辑 原始数据 按行查看 历史
Irony 提交于 2019-05-22 10:45 . 和Js交互操作
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Created on 2019年4月27日
@author: Irony
@site: https://pyqt5.com https://github.com/892768447
@email: 892768447@qq.com
@file: QWebEngineView.JsSignals
@description:
"""
import os
from time import time
from PyQt5.QtCore import QUrl, pyqtSlot, pyqtSignal
from PyQt5.QtWebKit import QWebSettings
from PyQt5.QtWebKitWidgets import QWebView
from PyQt5.QtWidgets import QMessageBox, QWidget, QVBoxLayout, QPushButton
__Author__ = 'Irony'
__Copyright__ = 'Copyright (c) 2019'
class WebView(QWebView):
customSignal = pyqtSignal(str)
def __init__(self, *args, **kwargs):
super(WebView, self).__init__(*args, **kwargs)
self.initSettings()
# 暴露接口对象
self.page().mainFrame().javaScriptWindowObjectCleared.connect(self._exposeInterface)
def _exposeInterface(self):
"""向Js暴露调用本地方法接口
"""
self.page().mainFrame().addToJavaScriptWindowObject('Bridge', self)
# 注意pyqtSlot用于把该函数暴露给js可以调用
@pyqtSlot(str)
def callFromJs(self, text):
QMessageBox.information(self, "提示", "来自js调用:{}".format(text))
def sendCustomSignal(self):
# 发送自定义信号
self.customSignal.emit('当前时间: ' + str(time()))
@pyqtSlot(str)
@pyqtSlot(QUrl)
def load(self, url):
'''
eg: load("https://pyqt5.com")
:param url: 网址
'''
return super(WebView, self).load(QUrl(url))
def initSettings(self):
'''
eg: 初始化设置
'''
# 获取浏览器默认设置
settings = self.settings()
# 开启开发人员工具
settings.setAttribute(QWebSettings.DeveloperExtrasEnabled, True)
# 设置默认编码
settings.setDefaultTextEncoding('UTF-8')
class Window(QWidget):
def __init__(self, *args, **kwargs):
super(Window, self).__init__(*args, **kwargs)
layout = QVBoxLayout(self)
self.webview = WebView(self)
layout.addWidget(self.webview)
layout.addWidget(QPushButton(
'发送自定义信号', self, clicked=self.webview.sendCustomSignal))
self.webview.windowTitleChanged.connect(self.setWindowTitle)
self.webview.load(QUrl.fromLocalFile(
os.path.abspath('Data/JsSignals.html')))
if __name__ == "__main__":
from PyQt5.QtWidgets import QApplication
import sys
app = QApplication(sys.argv)
w = Window()
w.show()
w.move(100, 100)
sys.exit(app.exec_())
Python
1
https://gitee.com/dushuren/PyQt.git
git@gitee.com:dushuren/PyQt.git
dushuren
PyQt
PyQt
master

搜索帮助