【发布时间】:2019-10-31 18:39:05
【问题描述】:
我本来想解决 this problem 的问题,但是当我测试了 QWebEnginePage::runJavaScript() 的行为后,我发现我什至无法通过以下代码使用 QWebEnginePage::runJavaScript() 更改 backgroundImage,那为什么呢?
import sys
from PyQt5.QtGui import *
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
from PyQt5.QtWebEngineWidgets import *
from PyQt5.QtWebEngineCore import *
class WebEngineView(QWebEngineView):
def __init__(self, parent=None):
super().__init__(parent)
self.webPage = self.page()
# self.webPage = WebEnginePage()
self.webPage.load(QUrl('https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/style'))
self.webPage.runJavaScript('''
window.addEventListener("load", function(event) {
alert(document.title);
document.body.style.backgroundImage = "url('https://www.w3schools.com/jsref/img_tree.png')";
});
''') # QWebEngineScript.MainWorld
if __name__ == "__main__":
app = QApplication(sys.argv)
app.setAttribute(Qt.AA_UseSoftwareOpenGL)
webEngineView = WebEngineView()
webEngineView.show()
sys.exit(app.exec_())
【问题讨论】:
标签: python pyqt pyqt5 qtwebengine qwebengineview