【问题标题】:Kivy app crash at start-up with Pyinstaller使用 Pyinstaller 启动 Kivy 应用程序崩溃
【发布时间】:2020-10-12 16:34:31
【问题描述】:

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

我的文件如下:

测试: 主文件 音乐 images_bg

当我使用第一个代码时:

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

然后我更新spec文件,在Collect中添加数据和树,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

【问题讨论】:

  • 您的应用程序中是否有硬编码help_bg.jpg 的路径?
  • 是的,路径是在应用程序上硬编码的。我添加了部分代码。

标签: python kivy pyinstaller


【解决方案1】:

您的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"

【讨论】:

  • 解决方案适用于此函数此代码 Pyinstaller --onefile --onedir --windowed --noconsole --clean main.py 但是当我尝试使用此代码获取单个文件时,有一个错误它被标记为致命错误 Pyinstaller --onefile --windowed --noconsole --clean main.py 只有一个文件,我们不能添加 Tree ('C:\Users\acer\Downloads\test\'),因为有没有收集
  • 请注意,这些命令中的任何一个都会覆盖main.spec。因此,您对main.spec 所做的任何更改都将丢失。通常,您运行一次Pyinstaller [options] main.py,然后编辑main.spec。之后你运行Pyinstaller main.spec。另请注意,当您使用spec 文件运行时,只有某些命令行选项有效。请参阅documentation
  • 我更新了我的问题。我不明白的是,当我想要一个文件时,我得到了这个错误:[WARNING] [AudioSDL2] Unable to load music / entry.wav: b'Mix_LoadWAV_RW with NULL src' [ERROR] [Image] Error reading file图像_bg / help_bg.jpg。你有想法吗?
  • 您是否清楚除非您运行Pyinstaller main.spec,否则您对main.spec 的更改将无效?此外,您需要以与图像类似的方式包含声音文件。要访问这些文件,您可能需要使用documentation 中描述的技术。
  • 我有一个问题:你知道如何使用 Builder.load_string 文件的相对路径吗?但我有这个错误i.stack.imgur.com/PoxZA.png
猜你喜欢
  • 2015-09-24
  • 2022-12-11
  • 1970-01-01
  • 2022-10-21
  • 2020-11-29
  • 1970-01-01
  • 2016-10-31
  • 1970-01-01
  • 2016-02-25
相关资源
最近更新 更多