python - QWebView.setHtml 不加载 CSS 个文件
python - QWebView.setHtml doesn't load CSS files
我有一个问题 运行 QWebView 使用包含 CSS 和 JS 文件的 HTML 文件。
我是 运行 pyqt5,在 windows 7 x64 上 Python 3.4。
例如这是我的 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>
我正在用 HTML 使用行
调用我的 QWebView
view.setHtml(open("test.html").read())
仅使用此代码我无法在 QWebView 中获取样式表和脚本 运行。
我使用行命令找到了 CSS 和 JS 文件的解决方案:
view.settings().setUserStyleSheetUrl(QUrl.fromLocalFile("style.css"))
对于 JS:
view.page().mainFrame().evaluateJavaScript(str(open("script.js").read()))
但是我找不到 css(或 HTML)文件中的字体或图像的解决方案。例如在 styles.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>
希望对您有所帮助。
我有一个问题 运行 QWebView 使用包含 CSS 和 JS 文件的 HTML 文件。
我是 运行 pyqt5,在 windows 7 x64 上 Python 3.4。
例如这是我的 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>
我正在用 HTML 使用行
调用我的 QWebViewview.setHtml(open("test.html").read())
仅使用此代码我无法在 QWebView 中获取样式表和脚本 运行。 我使用行命令找到了 CSS 和 JS 文件的解决方案:
view.settings().setUserStyleSheetUrl(QUrl.fromLocalFile("style.css"))
对于 JS:
view.page().mainFrame().evaluateJavaScript(str(open("script.js").read()))
但是我找不到 css(或 HTML)文件中的字体或图像的解决方案。例如在 styles.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>
希望对您有所帮助。