2016-11-16 5 views
1

QWebView (영감을받은 here)의 리플릿 맵을 표시하려고합니다. 다음과 같이 내 폴더의 구조는 같습니다PyQt5 QWebView : .js 파일을로드하는 .html 파일로드

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

내가 map.html 및 포함 map.js의 모든 컨텐츠 map.py를 실행 한 후 코드가 작동합니다. 내가로드하려고하면

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_() 

그러나 map.htmlQtCore.QUrl() 아무 반응이 없습니다. 내가 외부이 .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_() 

은 아무도 PyQt5에서 .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 파일을 통과하는 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); 

답변

1

사용 QUrl.fromLocalFile에 대한 코드 : 여기

map.html 코드입니다. 또한 파일의 절대 경로를 전달해야 할 것 같습니다.

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

이 방법이 유용합니까? 시도했지만 여전히 작동하지 않습니다. – dliv

+0

네, 작동하지만, 나는 단지'map.html'의 절대 경로를 사용하고 있다는 것을 깨달았습니다. 'map.html'만으로는 실패합니다. 나는 내 대답 – user3419537

+0

을 완벽하게 업데이트 할 것이고, 정말 고마워, 이제는 효과가있다. :) – dliv