forked from PyQt5/PyQt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
JsSignals.py
92 lines (73 loc) · 2.54 KB
/
JsSignals.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Created on 2019年4月27日
@author: Irony
@site: https://pyqt5.com https://github.com/892768447
@email: [email protected]
@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_())