How to include folium map into PyQt5 application window?

后端 未结 1 1590
既然无缘
既然无缘 2020-12-21 19:04

I would like to ask how do I go about including a folium map into PyQt 5 window application such that the map does not take up the whole window. I have found a similar post

相关标签:
1条回答
  • 2020-12-21 19:29

    The problem has nothing to do with a QWebEngineView or folium but how to place widgets inside the window, if so, then a solution is to use layouts in this case I will use the following structure: First a central widget is established, inside this one QHBoxLayout , and in the QHBoxLayout a QWidget is added as a container to the left side where a QVBoxLayout will be placed where the buttons will be, and to the right side the QWebEngineView:

    import io
    import sys
    
    import folium
    
    from PyQt5 import QtCore, QtGui, QtWidgets, QtWebEngineWidgets
    
    
    class Window(QtWidgets.QMainWindow):
        def __init__(self):
            super().__init__()
            self.initWindow()
    
        def initWindow(self):
            self.setWindowTitle(self.tr("MAP PROJECT"))
            self.setFixedSize(1500, 800)
            self.buttonUI()
    
        def buttonUI(self):
            shortPathButton = QtWidgets.QPushButton(self.tr("Find shortest path"))
            button2 = QtWidgets.QPushButton(self.tr("Another path"))
            button3 = QtWidgets.QPushButton(self.tr("Another path"))
    
            shortPathButton.setFixedSize(120, 50)
            button2.setFixedSize(120, 50)
            button3.setFixedSize(120, 50)
    
            self.view = QtWebEngineWidgets.QWebEngineView()
            self.view.setContentsMargins(50, 50, 50, 50)
    
            central_widget = QtWidgets.QWidget()
            self.setCentralWidget(central_widget)
            lay = QtWidgets.QHBoxLayout(central_widget)
    
            button_container = QtWidgets.QWidget()
            vlay = QtWidgets.QVBoxLayout(button_container)
            vlay.setSpacing(20)
            vlay.addStretch()
            vlay.addWidget(shortPathButton)
            vlay.addWidget(button2)
            vlay.addWidget(button3)
            vlay.addStretch()
            lay.addWidget(button_container)
            lay.addWidget(self.view, stretch=1)
    
            m = folium.Map(
                location=[45.5236, -122.6750], tiles="Stamen Toner", zoom_start=13
            )
    
            data = io.BytesIO()
            m.save(data, close_file=False)
            self.view.setHtml(data.getvalue().decode())
    
    
    if __name__ == "__main__":
        App = QtWidgets.QApplication(sys.argv)
        window = Window()
        window.show()
        sys.exit(App.exec())
    

    0 讨论(0)
提交回复
热议问题