复制自http://kivy.org/docs/guide/packaging-windows.html
创建规范文件
对于本示例,我们将打包 touchtracer 示例并嵌入自定义图标。 touchtracer 示例是 kivy\examples\demo\touchtracer 目录,主文件名为 main.py。
双击Kivy.bat,将打开一个控制台。
进入 pyinstaller 2.1 目录并创建初始规范:
cd pyinstaller-2.1
python pyinstaller.py --name touchtracer ..\kivy\examples\demo\touchtracer\main.py
您还可以将 icon.ico 文件添加到应用程序文件夹,以便为可执行文件创建图标。如果您没有可用的 .ico 文件,您可以使用 Web 应用程序 ConvertICO 将您的 icon.png 文件转换为 ico。将 icon.ico 保存在 touchtracer 目录中并输入:
python pyinstaller.py --name touchtracer --icon ..\kivy\examples\demo\touchtracer\icon.ico ..\kivy\examples\demo\touchtracer\main.py
有关更多选项,请参阅 PyInstaller 2 手册。
spec 文件将是 touchtracer.spec,位于 pyinstaller + touchtracer 目录中。现在我们需要编辑规范文件以添加 kivy 钩子以正确构建 exe。使用您喜欢的编辑器打开规范文件,并在规范的开头添加这些行:
from kivy.tools.packaging.pyinstaller_hooks import install_hooks
install_hooks(globals())
在Analysis() 函数中,删除hookspath=None 参数。如果你不这样做,kivy 包钩子将根本不会被使用。
然后您需要更改COLLECT() 调用以添加touchtracer 的数据(touchtracer.kv、particle.png,...)。更改该行以添加 Tree() 对象。此树将搜索 touchtracer 目录中找到的每个文件并将其添加到您的最终包中:
coll = COLLECT( exe, Tree('../kivy/examples/demo/touchtracer/'),
a.binaries,
#...
)
我们完成了。您的规范已准备好执行!
构建规范
双击Kivy.bat
转到 pyinstaller 目录,并构建规范:
cd pyinstaller-2.1
python pyinstaller.py touchtracer\touchtracer.spec
该包将位于 touchtracer\dist\touchtracer 目录中。