如何在 Python 中使用 QT 库显示 groundtrackplotter 的类似小部件的绘图

How to display groundtrackplotter's plot like widget with QT library in Python

我们开发的应用程序应该显示带有卫星和卫星轨道的地球地图。剧情是这样的

我们使用 python 和 PySide6.QT 进行 UI 操作,对于带卫星的地球地图绘图,我们使用 GroundTrackPotter。该库在浏览器中绘制。

我需要在中央小部件中放置绘图**(查看图片)

Class 应用

class Window(QWidget, QQmlApplicationEngine):
    def __init__(self):
        super().__init__()
        border_layout = BorderLayout()

        plot_3d = self.get_3d_lpot()
        border_layout.addWidget(plot_3d, Position.Center)

        satellite_dropdown = self.create_dropdown_satellite()
        border_layout.addWidget(satellite_dropdown, Position.West)

        satellite_info = self.get_satellite_info()
        border_layout.addWidget(satellite_info, Position.South)

        self.setLayout(border_layout)

        self.setWindowTitle("Satellite tracker")

    @staticmethod
    def create_label(text: str):
        label = QLabel(text)
        label.setFrameStyle(QFrame.Box | QFrame.Raised)
        return label

    @staticmethod
    def create_dropdown_satellite():
        widget = QComboBox()

        widget.addItems(["KITSUNE", "BEESAT", "ITUPSAT"])
        return widget

    @staticmethod
    def get_satellite_info():
        satellite_info = satellite_state.get_satellite_param_string()
        widget = QLabel(satellite_info)
        return widget

    @staticmethod
    def get_3d_lpot():
        *Here I need create widget for earth plot
        return widget

地球绘图:

gp = GroundtrackPlotter()


def get_orbit_plot(orbit: Orbit):
    # Build spacecraft instance
    satellite_spacecraft = EarthSatellite(orbit, None)
    t_span = time_range(start=orbit.epoch - 1.5 * u.h, periods=150, end=orbit.epoch + 1.5 * u.h)
    # Generate an instance of the plotter, add title and show latlon grid
    gp.update_layout(title="International Space Station groundtrack")

    # Plot previously defined EarthSatellite object
    gp.plot(
        satellite_spacecraft,
        t_span,
        label="Satellite",
        color="red",
        marker={"size": 10, "symbol": "triangle-right", "line": {"width": 1, "color": "black"}},
    )
    # For building geo traces
    import plotly.graph_objects as go

    # Faculty of Radiophysics and Computer Technologies coordinates
    STATION = [53.83821551524637, 27.476136409973797] * u.deg

    # Let us add a new trace in original figure
    gp.add_trace(
        go.Scattergeo(
            lat=STATION[0],
            lon=STATION[-1],
            name="Faculty of Radiophysics and Computer Technologies",
            marker={"color": "blue"},
        )
    )
    gp.fig.show()
    # Switch to three dimensional representation
    gp.update_geos(projection_type="orthographic")

    gp.fig.show()


def get_gp_value():
    return gp

您可以使用 Plotly 中的 Figure 生成 html 并将其嵌入到 QWebEngineView 中:

from astropy import units as u

from poliastro.earth import EarthSatellite
from poliastro.earth.plotting import GroundtrackPlotter
from poliastro.examples import iss
from poliastro.util import time_range

import plotly

from PySide6.QtWidgets import QApplication
from PySide6.QtWebEngineWidgets import QWebEngineView


def build_plot(fig):
    html = "".join(
        [
            "<html><body>",
            plotly.offline.plot(fig, output_type="div", include_plotlyjs="cdn"),
            "</body></html>",
        ]
    )
    return html


def create_plot():

    satellite_spacecraft = EarthSatellite(iss, None)
    t_span = time_range(iss.epoch - 1.5 * u.h, periods=150, end=iss.epoch + 1.5 * u.h)

    gp = GroundtrackPlotter()
    gp.update_layout(title="International Space Station groundtrack")

    # Plot previously defined EarthSatellite object
    gp.plot(
        satellite_spacecraft,
        t_span,
        label="Satellite",
        color="red",
        marker={"size": 10, "symbol": "triangle-right", "line": {"width": 1, "color": "black"}},
    )
    # For building geo traces
    import plotly.graph_objects as go

    # Faculty of Radiophysics and Computer Technologies coordinates
    STATION = [53.83821551524637, 27.476136409973797] * u.deg

    # Let us add a new trace in original figure
    gp.add_trace(
        go.Scattergeo(
            lat=STATION[0],
            lon=STATION[-1],
            name="Faculty of Radiophysics and Computer Technologies",
            marker={"color": "blue"},
        )
    )
    # Switch to three dimensional representation
    gp.update_geos(projection_type="orthographic")
    return gp.fig


def main():
    app = QApplication([])

    fig = create_plot()
    html = build_plot(fig)

    view = QWebEngineView()
    view.setHtml(html)
    view.resize(640, 480)
    view.show()

    app.exec()


if __name__ == "__main__":
    main()