PyQt和py2exe:编译到.ex后,QSound没有声音

2024-05-18 06:53:50 发布

您现在位置:Python中文网/ 问答频道 /正文

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

设置.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_应用程序.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. 资源

资源_qrc.qrc公司代码:

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

然后转换为资源.py使用pyrcc5

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

    QFile.copy(":/media/Siren.wav", "sounds/Siren.wav")
    sound = QSound("sounds/Siren.wav")
    sound.play() #still doesn't work after making exe!
    

在花了很多时间之后,我放弃了。在

任何帮助都将不胜感激。在


Tags: toplay资源exesirenfileworkwhen
3条回答

我使用python2.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")])

在python3.4.3、PyQt 5.5.1、py2exe0.9.2.2中,我也遇到了同样的问题。 问题不在错误的文件路径中。在

如果您打电话:

QAudioDeviceInfo.availableDevices(QAudio.AudioOutput)

从.exe,返回的列表将为空。在

您应该将foder:“\audio”从“site packages\PyQt5\plugins”添加到带有output.exe文件的目录中,这样声音就可以工作了。在

这是我的设置.py文件:

^{pr2}$

以及调用的示例代码设置.py功能:

#!/usr/bin/python3
# -*- coding: utf-8 -*-
import sys
from setup import get_name, get_exe_name, build

MAIN_MODULE = 'main.py'

exec('from ' + get_name(MAIN_MODULE) + ' import __version__, __appname__')
build(__version__, __appname__, MAIN_MODULE, dest_base=get_exe_name(__file__))

相关问题 更多 >