编译到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.pyfrom 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!
花了很多时间后,我放弃了。
任何帮助都将不胜感激。
答案 0 :(得分:1)
我使用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")])
答案 1 :(得分:1)
我也有同样的问题,Python 3.4.3,PyQt 5.5.1,py2exe 0.9.2.2。 问题不在错误的文件路径中。
如果你打电话:
iframe
来自.exe,返回的列表将为空。
你应该添加foder:&#34; \ audio&#34;来自&#34; site-packages \ PyQt5 \ plugins&#34;到输出.exe文件的目录,声音将起作用。
这是我的setup.py文件:
QAudioDeviceInfo.availableDevices(QAudio.AudioOutput)
调用setup.py函数的示例代码:
#!/usr/bin/python3
# -*- coding: utf-8 -*-
from distutils.core import setup
import py2exe
import sys
import os, os.path
import zipfile
import shutil
def get_exe_name(f):
dirname, filename = os.path.split(os.path.abspath(f))
return os.path.split(dirname)[-1].lower()
def get_name(name):
idx = name.find('.')
if idx != -1:
return name[:idx]
return name
def build(__version__, __appname__, main_module = 'main.py', dest_base='main', icon='images\\main.ico'):
#exec('from ' + get_name(main_module) + ' import __version__, __appname__')
try:
shutil.rmtree('dist')
except:
print ('Cann\'t remove "dist" directory: {0:s}'.format(str(sys.exc_info())))
if len(sys.argv) == 1:
sys.argv.append('py2exe')
options = {'optimize': 2,
# 'bundle_files': 0, # create singlefile exe 0
'compressed': 1, # compress the library archive
'excludes': ['pywin', 'pywin.debugger', 'pywin.debugger.dbgcon', 'pywin.dialogs', 'pywin.dialogs.list', 'os2emxpath', 'optparse', 'macpath', 'tkinter'],
'dll_excludes': ['w9xpopen.exe', 'mapi32.dll', 'mswsock.dll', 'powrprof.dll', 'MSVCP90.dll', 'HID.DLL'], # we don't need this
'includes': ['sip', 'locale', 'calendar', 'logging', 'logging.handlers', 'PyQt5', 'PyQt5.QtCore', 'PyQt5.QtGui', 'PyQt5.QtMultimedia', 'PyQt5.QtNetwork', 'PyQt5.QtPrintSupport'],
}
#datafiles = [('platforms', ['C:\\Python34\\Lib\\site-packages\\PyQt5\\plugins\\platforms\\qwindows.dll']),]
import PyQt5
datafiles = [('platforms', [PyQt5.__path__[0] + '\\plugins\\platforms\\qwindows.dll']), ('audio', [PyQt5.__path__[0] + '\\plugins\\audio\\qtaudio_windows.dll']),]
dirs = ['images', 'docs', 'ssl', 'sounds', 'p2ini', 'lib', 'config']
for d in dirs:
try:
for f in os.listdir(d):
f1 = d + '\\' + f
if os.path.isfile(f1): # skip directories
datafiles.append((d, [f1]))
except:
print ('Cann\'t find some files in directory "{0:s}": {1:s}'.format(d, str(sys.exc_info())))
setup(version = __version__,
description = __appname__,
name = '{0:s} {1:s} application'.format(__appname__, __version__),
options = {'py2exe': options},
zipfile = None,
data_files = datafiles,
windows = [{'icon_resources':[(1,'images\\main.ico')], 'script':main_module, 'dest_base':dest_base}] if icon else [{'script':main_module, 'dest_base':dest_base}],
scripts = [main_module],
#console = [{'icon_resources':[(1,'images\\main.ico')], 'script':main_module, 'dest_base':dest_base}] if icon else [{'script':main_module, 'dest_base':dest_base}],
)