使用Pyinstaller启动时Kivy应用程序崩溃

时间:2020-10-12 16:34:31

标签: python kivy pyinstaller

我将kivy 1.11.1与python 3.7.6 Windows 10和Pyinstall 4.0一起使用。我的应用程序运行完美,当我pyinstall pyinstall时,一切正常。但是,当我启动文件时,请在启动时执行崩溃应用程序。

部分代码:

from kivy.app import App
from kivy.base import EventLoop

from kivy.core.audio import SoundLoader

from kivy.utils import get_color_from_hex
from kivy.core.window import Window
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen



Builder.load_string("""
<HomeScreen>:
    BoxLayout:
        orientation: 'vertical'
        canvas.before:
            Color:
                rgba: (0.6, 0.6, 0.6, 0.8)
            Rectangle:
                pos: self.pos
                size: self.size
                source: "images_bg/help_bg.jpg"

""")


class Entry(Screen):
    pass
    


class MultiAudio:
    _next = 0

    def __init__(self, filename, count):
        self.buf = [SoundLoader.load(filename)
                    for i in range(count)]

    def play(self):
        self.buf[self._next].play()
        self._next = (self._next + 1) % len(self.buf)
    def stop(self):
        self.buf[self._next].stop()
        self._next = (self._next + 1) % len(self.buf)

entry = MultiAudio('music/entry.wav', 5)
        
# Create the screen manager
sm = ScreenManager()
sm.add_widget(Entry(name='entry'))


class EntryApp(App):
    def build(self):
        EventLoop.ensure_window()
        return sm

    Window.clearcolor = get_color_from_hex('111110') #('111110')   


app = EntryApp()
if __name__ == '__main__':
    app.run()

我的文件如下:

测试: main.py 音乐 images_bg

当我使用第一个代码时:

Pyinstaller --onefile --onedir --windowed --icon=test.ico --noconsole --clean main.py

然后我更新规格文件,将数据和树添加到“收集”中,该exe文件起作用。

main.spec:

# -- mode: python ; coding: utf-8 --
from kivy.tools.packaging.pyinstaller_hooks import install_hooks
install_hooks(globals())
from kivy_deps import sdl2, glew
import kivy.core.audio
import kivy.core.image

block_cipher = None

a = Analysis(['main.py'],
pathex=['C:\Users\acer\Downloads\test'],
binaries=[],
datas=[('C:\Users\acer\Downloads\test\images_bg', 'images_bg'),
],
hiddenimports=[],
runtime_hooks=[],
excludes=[],
win_no_prefer_redirects=False,
win_private_assemblies=False,
cipher=block_cipher,
noarchive=False)
pyz = PYZ(a.pure, a.zipped_data,
cipher=block_cipher)
exe = EXE(pyz,
a.scripts,
[],
exclude_binaries=True,
name='main',
debug=False,
bootloader_ignore_signals=False,
strip=False,
upx=True,
console=False , icon='test.ico')
coll = COLLECT(exe, Tree('C:\Users\acer\Downloads\test\'),
a.binaries,
a.zipfiles,
a.datas,
strip=False,
*[Tree(p) for p in (sdl2.dep_bins + glew.dep_bins)],
upx=True,
upx_exclude=[],
name='main')

Qaund我正在尝试使用以下代码创建一个文件:

Pyinstaller --onefile --icon=test.ico main.py

然后我更新规格文件:

# -*- mode: python ; coding: utf-8 -*-
from kivy_deps import sdl2, glew
import kivy.core.audio 
import kivy.core.image 


block_cipher = None


a = Analysis(['main.py'],
             pathex=['C:\Users\acer\Downloads\test'],
             binaries=[],
             datas=[('C:\Users\acer\Downloads\test\images_bg', 'images_bg')],
             hiddenimports=[],
             hookspath=[],
             runtime_hooks=[],
             excludes=[],
             win_no_prefer_redirects=False,
             win_private_assemblies=False,
             cipher=block_cipher,
             noarchive=False)
pyz = PYZ(a.pure, a.zipped_data,
             cipher=block_cipher)
exe = EXE(pyz,
          a.scripts,
          a.binaries,
          a.zipfiles,
          a.datas,
          *[Tree(p) for p in (sdl2.dep_bins + glew.dep_bins)],
          name='main',
          debug=False,
          bootloader_ignore_signals=False,
          strip=False,
          upx=True,
          upx_exclude=[],
          runtime_tmpdir=None,
          console=False , icon='test.ico')

启动时出现此错误:

[WARNING] [AudioSDL2   ] Unable to load music/entry.wav: b'Mix_LoadWAV_RW with NULL src'
[ERROR  ] [Image       ] Error reading file images_bg/help_bg.jpg
 Traceback (most recent call last):
   File "main.py", line 1642, in <module>
   File "kivy\uix\relativelayout.py", line 265, in __init__
   File "kivy\uix\floatlayout.py", line 65, in __init__
   File "kivy\uix\layout.py", line 76, in __init__
   File "kivy\uix\widget.py", line 361, in __init__
   File "kivy\uix\widget.py", line 469, in apply_class_lang_rules

1 个答案:

答案 0 :(得分:0)

您的datas行:

datas=[('C:\Users\acer\Downloads\test\images_bg', '.'),
],

应为:

datas=[('C:\Users\acer\Downloads\test\images_bg', 'images_bg'),
],

这会将图像放入与您的文件夹相对应的文件夹中。

source: "images_bg/help_bg.jpg"