[英]python - QWebView.setHtml doesn't load CSS files
我在使用包含 CSS 和 JS 文件的 HTML 文件运行 QWebView 时遇到问题。
我在 Windows 7 x64 上使用 Python 3.4 运行 pyqt5。
例如这里是我的 HTML 文件 test.html :
<html>
<head>
<title>A Sample Page</title>
<link rel="stylesheet" type="text/css" href="style.css">
<script type="text/javascript" src="script.js">
</head>
<body>
<h1>Hello, World!</h1>
<hr/>
I have nothing to say.
</body>
</html>
我正在使用 line 用 HTML 调用我的 QWebView
view.setHtml(open("test.html").read())
仅使用此代码我无法在 QWebView 中运行样式表和脚本。 我使用 line 命令找到了 CSS 和 JS 文件的解决方案:
view.settings().setUserStyleSheetUrl(QUrl.fromLocalFile("style.css"))
对于 JS :
view.page().mainFrame().evaluateJavaScript(str(open("script.js").read()))
但是,我在 css(或 HTML)文件中找不到字体或图像的解决方案。 例如在 style.css 中:
src:url('fonts/times/timesroman.eot');
我试过绝对路径,但还是不行。 任何人都可以帮忙吗?
提前致谢。
我在 5 小时搜索后找到了解决方案,在设置绝对路径之前我必须使用前缀 file:/// 所以我的例子是:
<html>
<head>
<title>A Sample Page</title>
<link rel="stylesheet" type="text/css" href="file:///C:/Users/....../style.css">
<script type="text/javascript" src="file:///C:/Users/....../script.js">
</head>
<body>
<h1>Hello, World!</h1>
<hr/>
I have nothing to say.
</body>
</html>
我希望这会有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.