简体   繁体   中英

Kivy app crash at start-up with Pyinstaller

I use kivy 1.11.1 with python 3.7.6 windows 10 and Pyinstall 4.0. My app works perfectly, and when I pyinstall pyinstall everything works properly. But when I launch the file, exe the crash application at startup.

Part of the code:

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()

My file is as follows:

test : main.py music images_bg

When I use this first code:

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

And then I update the spec file adding the datas and Tree in Collect, the exe file works.

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 I'm trying to have a single file with the following code:

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

And then I update the spec file:

# -*- 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')

I get this error on startup:

[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

Your datas line:

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

should be:

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

That will put the images in a folder that corresponds to your:

source: "images_bg/help_bg.jpg"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM