如何将Vue 3 GUI与Python代码连接以创建桌面应用程序?

0 投票
0 回答
43 浏览
提问于 2025-04-12 09:35

我正在尝试把一个 Vue 3 的界面和一个用 PyQt6 写的 Python 应用连接起来,但当我运行这个打包好的界面应用时,视图没有加载出来,尽管 Vue 本身似乎是正常工作的。在我用 XAMPP 测试的时候,应用运行得很好。我试过使用 CEF(Chromium 嵌入框架),但我的 Python 版本不支持这个,而且最后的更新是在两年前。有没有人能给我一些建议,告诉我该怎么做,或者我可能哪里做错了?我想我可能需要把这个界面作为一个 HTTP 服务器来运行。

版本信息:

Python: v3.12.0,

VueJS: v3.4.15

我的代码:

import json
import os
import sys
from PyQt6.QtCore import QUrl, QSize
from PyQt6.QtGui import QIcon
from PyQt6.QtWebEngineCore import QWebEnginePage
from PyQt6.QtWidgets import QApplication, QMainWindow
from PyQt6.QtWebEngineWidgets import QWebEngineView

# ================= # (Vue 3 - Client UI) # ================= #
# > | Vue 3 Client UI Localization
uiClientPath = os.path.join(os.getcwd(), 'vue-app', 'dist')
# ================= # (Vue 3 - Client UI) # ================= #


class App(QApplication):
    def __init__(self, sys_argv):
        super().__init__(sys_argv)

        # ================= # (Splash Screen - Sizes) # ================= #
        # > | Splash Screen Size
        self.main_window.resize(1024, 770)

        # > | Blocking Screen Stretching
        self.main_window.setFixedSize(QSize(1024, 770))
        # ================= # (Splash Screen - Sizes) # ================= #

        # ================= # (Client UI - Init) # ================= #
        # > | Load Client UI
        self.web_view = QWebEngineView()
        self.web_page = WebEnginePage()
        self.web_view.setPage(self.web_page)
        self.web_view.load(QUrl.fromLocalFile(os.path.join(uiClientPath, 'index.html')))
        self.main_window.setCentralWidget(self.web_view)

        self.main_window.show()
        # ================= # (Client UI - Init) # ================= #

def javascript_error_handler(type, value, traceback):
    print(f"JavaScript Error: {value}")
    print(f"JavaScript Error: {type.__name__}: {value}, {traceback}")

class WebEnginePage(QWebEnginePage):
    def javaScriptConsoleMessage(self, level, message, lineNumber, sourceID):
        #print(json.dumps(message))
        if isinstance(message, dict) or isinstance(message, list):
            errorMsg = json.dumps(message)
        else:
            errorMsg = str(message)
        print(f"JavaScript Console ({level}): {errorMsg} (line {lineNumber})")


if __name__ == "__main__":
    app = App(sys.argv)
    sys.excepthook = javascript_error_handler
    sys.exit(app.exec())

0 个回答

暂无回答

撰写回答