如果您正在使用,
setHtml()则如文档所示,外部资源将相对于您作为第二个参数传递的网址:
无效QWebEngineView :: setHtml(const QString&html,const QUrl&baseUrl =
QUrl())[…]
外部对象(例如HTML文档中引用的样式表或图像)相对于baseUrl定位。
[…]
因此,在您的情况下,解决方案是:
import osfrom PyQt5 import QtCore, QtGui, QtWidgets, QtWebEngineWidgetsif __name__ == "__main__": import sys sys.argv.append("--disable-web-security") app = QtWidgets.QApplication(sys.argv) wnd = QtWidgets.QWidget() genVLayout = QtWidgets.QVBoxLayout(wnd) verticalLayout_7 = QtWidgets.QVBoxLayout() webEngineViewGen = QtWebEngineWidgets.QWebEngineView(wnd) webEngineViewGen.setUrl(QtCore.QUrl("about:blank")) with open('main.html','r') as fh: html = fh.read() current_dir = os.path.dirname(os.path.abspath(__file__)) url = QtCore.QUrl.fromLocalFile(os.path.join(current_dir, "main.html")) webEngineViewGen.setHtml(html, url) verticalLayout_7.addWidget(webEngineViewGen) genVLayout.addLayout(verticalLayout_7) wnd.show() sys.exit(app.exec_())
或简单地使用
load()方法:
import osfrom PyQt5 import QtCore, QtGui, QtWidgets, QtWebEngineWidgetsif __name__ == "__main__": import sys sys.argv.append("--disable-web-security") app = QtWidgets.QApplication(sys.argv) wnd = QtWidgets.QWidget() genVLayout = QtWidgets.QVBoxLayout(wnd) verticalLayout_7 = QtWidgets.QVBoxLayout() webEngineViewGen = QtWebEngineWidgets.QWebEngineView(wnd) webEngineViewGen.setUrl(QtCore.QUrl("about:blank")) current_dir = os.path.dirname(os.path.abspath(__file__)) url = QtCore.QUrl.fromLocalFile(os.path.join(current_dir, "main.html")) webEngineViewGen.load(url) verticalLayout_7.addWidget(webEngineViewGen) genVLayout.addLayout(verticalLayout_7) wnd.show() sys.exit(app.exec_())
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)