PyQt5 QWebView: Load .html file 即加载.js文件

PyQt5 QWebView: Load .html file that loads .js file

我正在尝试展示带有 QWebView 的传单地图(灵感来自 here)。我的文件夹结构如下:

webkit_leaflet/
├── map.html
├── map.js
└── map.py

当我 运行 map.py 包含 map.htmlmap.js 的所有内容时,代码就可以工作了。

from PyQt5 import QtWidgets, QtWebKitWidgets
import sys


# Create application
app = QtWidgets.QApplication(sys.argv)

# Add window
win = QtWidgets.QWidget()
win.setWindowTitle('QWebView Map Test')

# Add layout
layout = QtWidgets.QVBoxLayout()
win.setLayout(layout)

# Create QWebView
view = QtWebKitWidgets.QWebView()

# include code from map.html and map.js

view.setHtml('''
<!DOCTYPE html>
<html>
<head>
    <link rel="stylesheet" href="http://cdn.leafletjs.com/leaflet-0.7.3/leaflet.css" />
    <script src="http://cdn.leafletjs.com/leaflet-0.7.3/leaflet.js"></script>
    <style>
        body { padding: 0; margin: 0; }
        html, body, #map { height: 100%; }
    </style>
</head>
<body>
    <div id="map"></div>
    <script>
        var map = L.map('map').setView([42.35, -71.08], 13);
        L.tileLayer('http://tiles.mapc.org/basemap/{z}/{x}/{y}.png',
        {
            maxZoom: 18,
            attribution: 'Map data &copy; <a href="http://openstreetmap.org">OpenStreetMap</a> contributors, ' +
                '<a href="http://creativecommons.org/licenses/by-sa/2.0/">CC-BY-SA</a>, ' +
                'Imagery &copy; <a href="http://mapbox.com">Mapbox</a>',
            id: 'examples.map-i86nkdio',
        }).addTo(map);
    </script>
</body>
</html>

''')

# Add QWebView to the layout
layout.addWidget(view)

# Show window, run app
win.show()
app.exec_()

但是,如果我尝试用 QtCore.QUrl() 加载 map.html,则没有任何反应。

from PyQt5 import QtCore, QtWidgets, QtWebKitWidgets
import sys


# Create application
app = QtWidgets.QApplication(sys.argv)

# Add window
win = QtWidgets.QWidget()
win.setWindowTitle('QWebView Map Test')

# Add layout
layout = QtWidgets.QVBoxLayout()
win.setLayout(layout)

# Create QWebView
view = QtWebKitWidgets.QWebView()

# load .html file
view.load(QtCore.QUrl('map.html'))

layout.addWidget(view)

win.show()
app.exec_()

当我从外部 .html 文件加载 JavaScript 文件时,有人能告诉我如何在 PyQt5 中显示 .html 文件的内容吗?

这是 map.html 的代码:

<!DOCTYPE html>
<html>
<head>
    <link rel="stylesheet" href="http://cdn.leafletjs.com/leaflet-0.7.3/leaflet.css" />
    <script src="http://cdn.leafletjs.com/leaflet-0.7.3/leaflet.js"></script>
    <style>
        body { padding: 0; margin: 0; }
        html, body, #map { height: 100%; }
    </style>
</head>
<body>
    <div id="map"></div>
    <script src="map.js"></script>
</body>
</html>

还有 map.js

的代码
var map = L.map('map').setView([42.35, -71.08], 13);
L.tileLayer('http://tiles.mapc.org/basemap/{z}/{x}/{y}.png',
{
    maxZoom: 18,
    attribution: 'Map data &copy; <a href="http://openstreetmap.org">OpenStreetMap</a> contributors, ' +
        '<a href="http://creativecommons.org/licenses/by-sa/2.0/">CC-BY-SA</a>, ' +
        'Imagery &copy; <a href="http://mapbox.com">Mapbox</a>',
    id: 'examples.map-i86nkdio',
}).addTo(map);

使用QUrl.fromLocalFile传递html文件。看来您还需要将绝对路径传递给文件。

import os
view.load(QtCore.QUrl.fromLocalFile(os.path.abspath('map.html')))