如何在 PyQt5 GUI 中显示 Folium 地图? [英] How to show Folium map inside a PyQt5 GUI?

查看:72
本文介绍了如何在 PyQt5 GUI 中显示 Folium 地图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 Qt GUI 中显示一个非常简单的 Folium 地图.主要代码为:

I'm trying to show a very simple Folium map inside a Qt GUI. The main code is:

import folium

m = folium.Map(location=[45.5236, -122.6750])
m
m.save('index.html')
folium.Map(
    location=[45.5236, -122.6750],
    tiles='Stamen Toner',
    zoom_start=13
)

当我将代码与 Jupyter 一起使用时,它很好,但会在 Spyder 中显示任何内容.我想要的是在简单的 Qt GUI 上的 QGraphicsView 或任何其他 QClass 中显示地图?

When I use the code with Jupyter it's fine but shows anything with Spyder. What I want is to show the map in a QGraphicsView or any other QClass on a simple Qt GUI?

推荐答案

您可以使用 save 方法将 html 保存在 io.BytesIO() 中,然后使用 setHtml() 方法将其设置为 QWebEngineView:

You can save the html in a io.BytesIO() using the save method and then set it to a QWebEngineView using the setHtml() method:

import io
import sys

import folium
from PyQt5 import QtWidgets, QtWebEngineWidgets

if __name__ == "__main__":
    app = QtWidgets.QApplication(sys.argv)
    m = folium.Map(
        location=[45.5236, -122.6750], tiles="Stamen Toner", zoom_start=13
    )

    data = io.BytesIO()
    m.save(data, close_file=False)

    w = QtWebEngineWidgets.QWebEngineView()
    w.setHtml(data.getvalue().decode())
    w.resize(640, 480)
    w.show()

    sys.exit(app.exec_())

这篇关于如何在 PyQt5 GUI 中显示 Folium 地图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆