如何讓PyQt5中QWebEngineView與JavaScript交互
準(zhǔn)備工作
開發(fā)環(huán)境
- Python 3.8.1
- Windows 10
安裝依賴
pip install PyQt5 pip install PyQtWebEngine
Python端
1.使用QWebChannel的registerObject("JsBridge名","JsBridge")方法注冊回調(diào)
- JsBridge名:在JavaScript中調(diào)用時(shí)使用的對象名稱
- JsBridge:被JavaScript調(diào)用的Python對象
2.JsBridge 對象
- 入?yún)?/li>
@QtCore.pyqtSlot(str) def log(self, message): print(message)
- 出參
@QtCore.pyqtSlot(result=str) def getName(self): return "hello"
- 出入?yún)?/li>
@QtCore.pyqtSlot(str, result=str) def test(self, message): print(message) return "ok"
JavaScript端
在Html的<head>中添加
<script src='qrc:///qtwebchannel/qwebchannel.js'></script>
調(diào)用
new QWebChannel(qt.webChannelTransport, function(channel) {
channel.objects.pythonBridge.test("hello",function(arg) {
console.log("Python message: " + arg);
alert(arg);
});
});
調(diào)試(Chrome DevTools)
- 配置環(huán)境變量:QTWEBENGINE_REMOTE_DEBUGGING = port
- 使用Chromium內(nèi)核的瀏覽器打開地址:http://127.0.0.1:port
- 使用PyCharm中可以在運(yùn)行配置(Run/Debug Configurations)中的Environment variables中添加環(huán)境變量,用;號(hào)分隔,然后可以直接運(yùn)行。
Demo
Python
1.JsBridge
from PyQt5 import QtCore
class JsBridge(QtCore.QObject):
@QtCore.pyqtSlot(str, result=str)
def test(self, message):
print(message)
return "ok"
2.Application
from PyQt5 import QtCore
from PyQt5 import QtWebEngineWidgets
from PyQt5.QtCore import QDir
from PyQt5.QtWebChannel import QWebChannel
from PyQt5.QtWebEngineWidgets import QWebEngineView
from PyQt5.QtWidgets import *
class TestWindow(QMainWindow):
def __init__(self):
super().__init__()
self.webView = QWebEngineView()
self.webView.settings().setAttribute(
QtWebEngineWidgets.QWebEngineSettings.JavascriptEnabled, True)
channel = QWebChannel(self.webView.page())
self.webView.page().setWebChannel(channel)
self.python_bridge = JsBridge(None)
channel.registerObject("pythonBridge", self.python_bridge)
layout = QVBoxLayout()
layout.addWidget(self.webView)
widget = QWidget()
widget.setLayout(layout)
self.setCentralWidget(widget)
self.resize(900, 600)
self.setWindowTitle('Test')
qr = self.frameGeometry()
cp = QDesktopWidget().availableGeometry().center()
qr.moveCenter(cp)
self.move(qr.topLeft())
self.show()
html_path = QtCore.QUrl.fromLocalFile(QDir.currentPath() + "/assets/index.html")
self.webView.load(html_path)
if __name__ == '__main__':
app = QApplication(sys.argv)
m = TestWindow()
sys.exit(app.exec_())
JavaScript
index.html
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
<title>Test</title>
<script src='qrc:///qtwebchannel/qwebchannel.js'></script>
<script src="https://code.jquery.com/jquery-3.4.1.min.js"
integrity="sha256-CSXorXvZcTkaix6Yvo6HppcZGetbYMGWSFlBw8HfCJo="
crossorigin="anonymous"></script>
</head>
<body>
<button id="test">test</button>
</body>
<script>
$(document).ready(function() {
new QWebChannel(qt.webChannelTransport, function(channel) {
$('#test').on('click', function() {
channel.objects.pythonBridge.test("hello",function(arg) {
console.log("Python message: " + arg);
alert(arg);
});
});
});
});
</script>
</html>
本文作者: liaoheng
本文鏈接: https://liaoheng.me/2019/12/23/PyQt5-QWebEngineView與JavaScript交互/
以上就是如何讓PyQt5中QWebEngineView與JavaScript交互的詳細(xì)內(nèi)容,更多關(guān)于QWebEngineView與JavaScript交互的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
解決Python3.8用pip安裝turtle-0.0.2出現(xiàn)錯(cuò)誤問題
turtle庫是python的基礎(chǔ)繪圖庫,這個(gè)庫被介紹為一個(gè)最常用的用來給孩子們介紹編程知識(shí)的方法庫,這篇文章主要介紹了解決Python3.8用pip安裝turtle-0.0.2出現(xiàn)錯(cuò)誤問題,需要的朋友可以參考下2020-02-02
python3下pygame如何實(shí)現(xiàn)顯示中文
這篇文章主要介紹了python3下pygame如何實(shí)現(xiàn)顯示中文,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-01-01
python?實(shí)現(xiàn)?mp3Play?音頻播放
這篇文章主要介紹了python?實(shí)現(xiàn)?mp3Play?音頻播放,文章基于python的相關(guān)資料展開詳細(xì)內(nèi)容,具有一定的參考價(jià)值需要的小伙伴可以參考一下2022-04-04
python 計(jì)算兩個(gè)列表的相關(guān)系數(shù)的實(shí)現(xiàn)
這篇文章主要介紹了python 計(jì)算兩個(gè)列表的相關(guān)系數(shù)的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-08-08
一文教你如何用Python輕輕松松操作Excel,Word,CSV
數(shù)據(jù)處理是 Python 的一大應(yīng)用場景,而 Excel 又是當(dāng)前最流行的數(shù)據(jù)處理軟件。本文將為大家詳細(xì)介紹一下如何用Python輕輕松松操作Excel、Word、CSV,需要的可以參考一下2022-02-02
Pytorch修改ResNet模型全連接層進(jìn)行直接訓(xùn)練實(shí)例
在本篇文章里小編給大家整理的是關(guān)于Pytorch修改ResNet模型全連接層進(jìn)行直接訓(xùn)練相關(guān)知識(shí)點(diǎn),有需要的朋友們參考下。2019-09-09
python使用post提交數(shù)據(jù)到遠(yuǎn)程url的方法
這篇文章主要介紹了python使用post提交數(shù)據(jù)到遠(yuǎn)程url的方法,涉及Python使用post傳遞數(shù)據(jù)的相關(guān)技巧,需要的朋友可以參考下2015-04-04
pandas創(chuàng)建series的三種方法小結(jié)
這篇文章主要介紹了pandas創(chuàng)建series的三種方法小結(jié),具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-05-05

