首页 > 解决方案 > 我可以在 PyQt5 QWebEngine 中将 Python 连接到 JavaScript 吗?

问题描述

我已经设置了一个基本的 PyQt5 GUI 窗口,中间有一个 QWebEngine 网络浏览器元素,我想要一个很好的方法来设置以下内容:

我希望网络浏览器显示一个本地 HTML 文件,当你按下页面上的某个按钮时,它会调用一个 python 函数,该函数会依次更新 html 中的一些内容。

这就是我到目前为止所得到的;我尝试了很多不同的方法,但它们似乎都是针对 PyQt4 而不是使用 QWebEngine。

import sys, os
import PyQt5
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtWebEngineWidgets import *


class WindowC(PyQt5.QtWidgets.QMainWindow):

    def __init__(self, *args, **kwargs):
        super(WindowC,self).__init__(*args, **kwargs)

        self.directory = os.getcwd()

        self.browser = QWebEngineView()
        self.setCentralWidget(self.browser)

        local_url = QUrl.fromLocalFile( os.path.join( self.directory, "page.html" ) )
        self.browser.load(local_url)


app = PyQt5.QtWidgets.QApplication(sys.argv)
window = WindowC()
window.show()
app.exec_()

对不起,如果问题有点模糊

标签: javascriptpythonpyqtpyqt5qwebenginepage

解决方案


推荐阅读