如何将JS地图添加到PYQT5?

发布于 2025-02-09 02:22:36 字数 1758 浏览 1 评论 0原文

我想将Marinetraffic提供的地图添加到Pyqt5中。当我将Marinetraffic提供的HTML代码添加到自己的程序中时,它无效。 我想添加的地图: Marinetraffic Map JS js

from PyQt5 import QtCore, QtGui, QtWidgets, QtWebEngineWidgets, QtWebChannel


class Backend(QtCore.QObject):
    valueChanged = QtCore.pyqtSignal(str)

    def __init__(self, parent=None):
        super().__init__(parent)
        self._value = ""

    @QtCore.pyqtProperty(str)
    def value(self):
        return self._value

    @value.setter
    def value(self, v):
        self._value = v
        self.valueChanged.emit(v)


class Widget(QtWidgets.QWidget):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.webEngineView = QtWebEngineWidgets.QWebEngineView()
        self.label = QtWidgets.QLabel(alignment=QtCore.Qt.AlignCenter)

        lay = QtWidgets.QVBoxLayout(self)
        lay.addWidget(self.webEngineView, stretch=1)
        lay.addWidget(self.label, stretch=1)

        backend = Backend(self)
        backend.valueChanged.connect(self.label.setText)
        backend.valueChanged.connect(self.foo_function)
        self.channel = QtWebChannel.QWebChannel()
        self.channel.registerObject("backend", backend)
        self.webEngineView.page().setWebChannel(self.channel)

        path = "index.html"
        self.webEngineView.setUrl(QtCore.QUrl.fromLocalFile(path))

    @QtCore.pyqtSlot(str)
    def foo_function(self, value):
        print(value)


if __name__ == "__main__":
    import sys

    app = QtWidgets.QApplication(sys.argv)
    w = Widget()
    w.show()
    sys.exit(app.exec_())

当我运行它时,我会得到连接失败的错误。 由于我的搜索,我尝试了所有尝试的方法,我在哪里做错了,您可以帮忙吗?

I want to add the map provided by Marinetraffic to pyqt5. When I add the HTML codes provided by MarineTraffic to my own program, it doesn't work.
The map I want to add:
MarineTraffic Map JS

from PyQt5 import QtCore, QtGui, QtWidgets, QtWebEngineWidgets, QtWebChannel


class Backend(QtCore.QObject):
    valueChanged = QtCore.pyqtSignal(str)

    def __init__(self, parent=None):
        super().__init__(parent)
        self._value = ""

    @QtCore.pyqtProperty(str)
    def value(self):
        return self._value

    @value.setter
    def value(self, v):
        self._value = v
        self.valueChanged.emit(v)


class Widget(QtWidgets.QWidget):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.webEngineView = QtWebEngineWidgets.QWebEngineView()
        self.label = QtWidgets.QLabel(alignment=QtCore.Qt.AlignCenter)

        lay = QtWidgets.QVBoxLayout(self)
        lay.addWidget(self.webEngineView, stretch=1)
        lay.addWidget(self.label, stretch=1)

        backend = Backend(self)
        backend.valueChanged.connect(self.label.setText)
        backend.valueChanged.connect(self.foo_function)
        self.channel = QtWebChannel.QWebChannel()
        self.channel.registerObject("backend", backend)
        self.webEngineView.page().setWebChannel(self.channel)

        path = "index.html"
        self.webEngineView.setUrl(QtCore.QUrl.fromLocalFile(path))

    @QtCore.pyqtSlot(str)
    def foo_function(self, value):
        print(value)


if __name__ == "__main__":
    import sys

    app = QtWidgets.QApplication(sys.argv)
    w = Widget()
    w.show()
    sys.exit(app.exec_())

When I run it, I get a connection failed error.
As a result of my searches, I get the same error in all the methods I tried, where am I doing wrong, can you help?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

冷默言语 2025-02-16 02:22:36

阅读 qtcore.qurl.fromlocalfile 的文档:

qtcore.qurl.qurl.fromlocalfile

“一个具有相对路径的文件URL仅在解决基本网址时才有意义。”

因此,我们添加了基本路径:

import os
...

        path = os.getcwd() + "\\index.html"
        self.webEngineView.setUrl(QtCore.QUrl.fromLocalFile(path))

在OS之间添加了路径兼容性(编辑)

from pathlib import Path
...
        base_path = Path(Path.cwd())
        full_path = base_path.joinpath('index.html')
        self.webEngineView.setUrl(QtCore.QUrl.fromLocalFile(str(full_path)))

Read the documentation for QtCore.QUrl.fromLocalFile :

QtCore.QUrl.fromLocalFile

"A file URL with a relative path only makes sense if there is a base URL to resolve it against."

So we add the base path:

import os
...

        path = os.getcwd() + "\\index.html"
        self.webEngineView.setUrl(QtCore.QUrl.fromLocalFile(path))

Added path compatibility between os (edited)

from pathlib import Path
...
        base_path = Path(Path.cwd())
        full_path = base_path.joinpath('index.html')
        self.webEngineView.setUrl(QtCore.QUrl.fromLocalFile(str(full_path)))

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文