我想问一下如何在PyQt 5窗口应用程序中包含一个叶形贴图,以便该贴图不会占用整个窗口。我在StackOverflow上找到了类似的文章“如何在PyQt5 GUI中显示Folium映射 ”,但是,所提供的解决方案代码显示了folium映射占用了整个PyQt 5窗口应用程序。
因此,我的问题是我如何包含叶面贴图,但仅占用PyQt 5窗口应用程序的一部分?如下所示,我试图将地图包括在矩形区域中。*矩形黑匣子在油漆上绘制,仅供参考。
仅供参考, 我已经尝试了帖子中的解决方案代码,但似乎无法调整地图的大小。
想要的输出
当前参考代码
from PyQt5.QtWidgets import QMainWindow, QApplication, QPushButton from PyQt5 import QtWebEngineWidgets import sys from PyQt5 import QtGui from PyQt5.QtCore import QRect class Window(QMainWindow): def __init__(self): super().__init__() self.title = "MAP PROJECT" self.left = 200 self.top = 100 self.width = 1500 self.height = 800 self.initWindow() def initWindow(self): # set window title self.setWindowTitle(self.title) # set window geometry # self.setGeometry(self.left, self.top, self.width, self.height) # Disable PyQt 5 application from resizing self.setFixedSize(self.width, self.height) self.buttonUI() self.show() def buttonUI(self): shortPathButton = QPushButton("Find shortest path", self) # (set button location (x, x) set button size (y, y) shortPathButton.setGeometry(QRect(30, 300, 120, 50)) button2 = QPushButton("Another path", self) # (set button location (x, x) set button size (y, y) button2.setGeometry(QRect(30, 370, 120, 50)) button3 = QPushButton("Another path", self) # (set button location (x, x) set button size (y, y) button3.setGeometry(QRect(30, 440, 120, 50)) # Below code is to connect the button to the function # button.clicked.connect(self.ClickMe) # Create function for shortest path (A* algorithm) """def ClickMe(self): print("Hello World")""" if __name__ == "__main__": App = QApplication(sys.argv) window = Window() sys.exit(App.exec())
这个问题与QWebEngineView或folium没有关系,但是如何在窗口内放置小部件,如果是这样,那么一种解决方案是在这种情况下使用布局,我将使用以下结构:首先在此内部建立一个中央小部件QHBoxLayout,并在QHBoxLayout中将QWidget作为容器添加到左侧的QVBoxLayout(将放置按钮的位置)和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())