gpt4 book ai didi

python - PyQt 和 py2exe : no sound from QSound after compiling to . exe

转载 作者:行者123 更新时间:2023-12-01 04:46:45 27 4
gpt4 key购买 nike

编译为 exe 后,我在使用 QSound.play() 播放 .wav 声音时遇到问题(我使用的是 Python 3.4.3、PyQt 5.4.1 和 py2exe 0.9.2.0)。

setup.py代码:

from distutils.core import setup
import py2exe

setup(
windows=[
{
"script": "main_app.py",
"icon_resources": [(0, "favicon163248.ico")]
}
],
data_files=[
(
'sounds', ['sounds\Siren.wav']

)
],
options={"py2exe": {"includes": ["sip"], "dist_dir": "MyProject"}}
)

我尝试过什么:

  1. 相对路径

    sound = QSound("sounds/Siren.wav") 
    sound.play() #works when simply running, doesn't work when compiling to exe
  2. 可执行文件的路径 (main_app.exe)

    sound = QSound(os.path.dirname(sys.executable) + "\sounds\Siren.wav")
    sound.play() #doesn't work when compiling to exe
  3. 绝对路径

    sound = QSound("C:\\path\\to\\project\\MyProject\\sounds\\Siren.wav") 
    sound.play() #works when simply running, doesn't work when compiling to exe
  4. 资源

resources_qrc.qrc代码:

<RCC>
<qresource prefix="media">
<file>Siren.wav</file>
<file>favicon163248.ico</file>
</qresource>
</RCC>

然后使用pyrcc5转换为resources.py

from resources import *
...
sound = QSound(':/media/Siren.wav')
sound.play() #works when simply running, doesn't work when compiling to exe
  • 将表单资源即时复制到硬盘

    QFile.copy(":/media/Siren.wav", "sounds/Siren.wav")
    sound = QSound("sounds/Siren.wav")
    sound.play() #still doesn't work after making exe!
  • 在花了相当多的时间之后,我放弃了。

    如有任何帮助,我们将不胜感激。

    最佳答案

    我使用 python 2.7 和 cx_Freeze 4.3.1 以及 PyQt4

    #-*- coding: utf-8-*-
    __author__ = 'Aaron'
    from cx_Freeze import setup, Executable
    import sys

    if sys.platform == "win32":
    base = "Win32GUI"


    includefiles= ['icons','Sound','imageformats']


    includes = ['sip', 'PyQt4.QtCore']


    setup(
    name = u"Your Programe",
    version = "1.0",
    description = u"XXXX",
    options = {'build_exe': {'include_files':includefiles}},
    executables = [Executable("Your programe name.py" ,base = base, icon = "XXX.ico")])

    关于python - PyQt 和 py2exe : no sound from QSound after compiling to . exe,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29256724/

    27 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com