javascript - 如何在 pyside qwebview 中从本地计算机加载 javascript?

标签 javascript python pyside qwebview

我的文件

messi_fan.py
barcelona_fan.html
jq.js

在同一目录中。

messi_fan.py

        f = open('barcelona_fan.html', 'r')
        html = f.read()
        f.close()
        self.webView = QWebView()
        self.webView.setHtml(html, baseUrl=QUrl('http://local'))
        self.webView.show()

barcelona_fan.html

<script type="text/javascript" src = "jq.js"></script>

我的问题是 jquery 没有在 Qwebview 中加载。如果我在 barcelona_fan.html 中这样使用

<script type="text/javascript" src = "http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js"></script>

一切都会好起来的。但我想从本地加载 .js 文件。我该怎么做?

最佳答案

让baseUrl查找本地目录:

path = "c:\\foo\\bar"
self.webView.setHtml(html, baseUrl = QUrl().fromLocalFile(path))

顺便说一句:如果需要,请不要忘记:

self.webView.settings().setAttribute(QWebSettings.LocalContentCanAccessRemoteUrls, True)

关于javascript - 如何在 pyside qwebview 中从本地计算机加载 javascript?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16222049/

相关文章:

javascript - 使用 jquery 检查输入字段的内容

python - 透明效果python图像库

python - Django:在通用 View 中提供下载服务

python - 如何在 virtualenv 中调试 pyside 段错误?

python - 无法访问 UI 文件中的 QWidget,PySide

javascript - 如何在没有 NodeJs 的情况下保持登录 Gmail api?

javascript - 我需要使用 JavaScript 可视化动态集合,可以使用响应式扩展来实现吗?

javascript - 计算嵌套 Json 内的唯一值

python - 二维数组中元素比较的算法

python - 使用 python 将 Qt 与 Windows 7 任务栏集成?