【发布时间】:2015-03-25 13:14:23
【问题描述】:
编译为 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"}}
)
我尝试了什么:
-
相对路径
sound = QSound("sounds/Siren.wav") sound.play() #works when simply running, doesn't work when compiling to exe -
可执行文件的路径 (main_app.exe)
sound = QSound(os.path.dirname(sys.executable) + "\sounds\Siren.wav") sound.play() #doesn't work when compiling to exe -
绝对路径
sound = QSound("C:\\path\\to\\project\\MyProject\\sounds\\Siren.wav") sound.play() #works when simply running, doesn't work when compiling to exe - 资源
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!
花了相当多的时间之后,我放弃了。
任何帮助将不胜感激。
【问题讨论】:
-
我可以使用 cxfreeze 玩
QSound。也许你可以试试。 -
@Aaron 我对 cxfreeze 也有同样的问题。能给我举个例子吗?
标签: python pyqt pyqt4 py2exe pyqt5