MacOS 不显示与 QTWebEngine 一起使用的窗口

问题描述 投票:0回答:1

我正在用Python开发一个应用程序来帮助使用抽认卡进行学习,这个应用程序应该在MacOS和Windows上运行。问题是我用 pyinstaller 编译我的应用程序,在 Windows 中它工作得很好,但是当我为 MacOS 编译它时(我使用 Github Workflow,因为我没有 Mac),它给我带来了 QWebengine 进程的问题,首先是它没有在正确的路径中找到它们并且应用程序崩溃,我通过添加以下内容解决了这个问题:

if platform.system() == "Darwin":
   os.environ['QTWEBENGINEPROCESS_PATH'] = os.path.join(os.path.dirname(sys.argv[0]),'Contents', 'Frameworks', 'PySide6', 'lib', 'QtWebEngineCore.framework', 'Helpers', 'QtWebEngineProcess.app', 'Contents', 'MacOS')
   os.environ['QTWEBENGINE_RESOURCES_PATH'] = (os.path.dirname(sys.argv[0]),'Contents', 'Frameworks', 'PySide6', 'lib', 'QtWebEngineCore.framework', 'Resources')

应用程序突然关闭的问题得到了解决,但现在它不会生成应该使用此代码生成的窗口:

import os
import sys
from PySide6.QtWidgets import QApplication, QMainWindow, QDockWidget
from PySide6.QtWebEngineWidgets import QWebEngineView
from PySide6.QtWebEngineCore import QWebEngineSettings, QWebEnginePage
from PySide6.QtCore import QUrl, Qt, QEventLoop
import relativeroute

class SpotifyPlayerWindow(QMainWindow):
    def __init__(self, parent=None):
        super().__init__(parent)

        # Window's configuration 
        self.setWindowTitle("Spotify Player")
        self.setGeometry(1040, 735, 499, 80)
        self.setWindowFlags(Qt.FramelessWindowHint)

        # Create the web viewer
        self.browser = QWebEngineView(self)
        html_path = os.path.join(relativeroute.resource_path("websarchives/spotify_player.html"))

        # Disable local security (so that html loads correctly)
        self.browser.settings().setAttribute(QWebEngineSettings.LocalContentCanAccessRemoteUrls, True)
        self.browser.settings().setAttribute(QWebEngineSettings.LocalStorageEnabled, True)
        self.browser.settings().setAttribute(QWebEngineSettings.AllowRunningInsecureContent, True)
        self.browser.settings().setAttribute(QWebEngineSettings.PlaybackRequiresUserGesture, False)

        # Connect the `loadFinished` signal to a function that terminates the wait loop
        self.browser.loadFinished.connect(self.on_page_load)

        # Load HTML page
        self.browser.setUrl(QUrl.fromLocalFile(html_path))

        # Set the browser as a central widget
        self.setCentralWidget(self.browser)


        # Wait until the page is fully loaded
        self.wait_until_loaded()

        self.start_music()

    def wait_until_loaded(self):
        """Wait until the page is fully loaded"""
        self.page_loaded = False
        loop = QEventLoop()  #Create temporary event loop

        # Exit the event loop when the page finishes loading
        self.browser.loadFinished.connect(loop.quit)
        loop.exec_()  # Run the event loop until 'loop.quit' is called

    def on_page_load(self):
        """Triggered when the page finishes loading"""
        self.page_loaded = True
        print("The page has loaded successfully The page has loaded successfully ")

    def play_music(self):
        
        if self.page_loaded:
            self.browser.page().runJavaScript("playMusic()")

    def pause_music(self):
        
        if self.page_loaded:
            self.browser.page().runJavaScript("pauseMusic()")

    def start_music(self):
        
        if self.page_loaded:
            self.browser.page().runJavaScript("startMusic()")

if __name__ == '__main__':
    app = QApplication(sys.argv)
    window = SpotifyPlayerWindow()
    window.show()
    
    sys.exit(app.exec())

已验证您要激活音乐的设置已保存并且 文件已就位。

它应该使用本地 文件打开一个窗口,但它甚至不会生成网站应该出现的窗口。

python-3.x macos qwebengine
1个回答
0
投票

Según Mis avanzados conocimientos recomiendo q pruebes a espabilar

© www.soinside.com 2019 - 2024. All rights reserved.