Javascript PyQt5 QWebView:加载.js文件的Load.html文件
我正试图用Javascript PyQt5 QWebView:加载.js文件的Load.html文件,javascript,pyqt,pyqt5,qwebview,qwebkit,Javascript,Pyqt,Pyqt5,Qwebview,Qwebkit,我正试图用QWebView(灵感来源)展示传单地图。“我的文件夹”的结构如下所示: webkit_leaflet/ ├── map.html ├── map.js └── map.py 当我运行map.py并包含map.html和map.js中的所有内容时,代码就工作了 from PyQt5 import QtWidgets, QtWebKitWidgets import sys # Create application app = QtWidgets.QApplication(sys.a
QWebView
(灵感来源)展示传单地图。“我的文件夹”的结构如下所示:
webkit_leaflet/
├── map.html
├── map.js
└── map.py
当我运行map.py
并包含map.html
和map.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 © <a href="http://openstreetmap.org">OpenStreetMap</a> contributors, ' +
'<a href="http://creativecommons.org/licenses/by-sa/2.0/">CC-BY-SA</a>, ' +
'Imagery © <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_()
当我从外部.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>
用于传递html文件。似乎还需要传递文件的绝对路径
import os
view.load(QtCore.QUrl.fromLocalFile(os.path.abspath('map.html')))
这对你有用吗?我试过了,但仍然不起作用。是的,它起作用了,但我刚刚意识到我使用的是
map.html
的绝对路径。只需map.html
它就会失败。我会更新我的答案
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 © <a href="http://openstreetmap.org">OpenStreetMap</a> contributors, ' +
'<a href="http://creativecommons.org/licenses/by-sa/2.0/">CC-BY-SA</a>, ' +
'Imagery © <a href="http://mapbox.com">Mapbox</a>',
id: 'examples.map-i86nkdio',
}).addTo(map);
import os
view.load(QtCore.QUrl.fromLocalFile(os.path.abspath('map.html')))