exe编译的Kivy程序访问图像的问题

时间:2016-06-04 09:02:25

标签: python python-3.x kivy pyinstaller

所以我创建了一个简单的程序,用自定义图像替换滑块光标背景,并尝试使用PyInstaller参数使用-F编译它(将其编译为单个exe )。编译成功,但是当我尝试执行它时,无法使用以下错误日志加载图像:

[WARNING           ] [Image       ] Unable to load image <rogue.jpg>
 Traceback (most recent call last):
   File "site-packages\kivy\lang\builder.py", line 822, in _build_canvas
   File "kivy\graphics\instructions.pyx", line 362, in kivy.graphics.instruction
s.VertexInstruction.source.__set__ (kivy\graphics\instructions.c:7177)
   File "kivy\graphics\context_instructions.pyx", line 381, in kivy.graphics.con
text_instructions.BindTexture.source.__set__ (kivy\graphics\context_instructions
.c:7309)
   File "site-packages\kivy\core\image\__init__.py", line 528, in __init__
   File "site-packages\kivy\core\image\__init__.py", line 716, in _set_filename
   File "site-packages\kivy\core\image\__init__.py", line 432, in load
   File "site-packages\kivy\core\image\__init__.py", line 200, in __init__

   File "site-packages\kivy\core\image\img_sdl2.py", line 41, in load
 Exception: SDL2: Unable to load image

 During handling of the above exception, another exception occurred:

 Traceback (most recent call last):
   File "untitled-1.py", line 21, in <module>
     TestApp().run()
   File "site-packages\kivy\app.py", line 802, in run
   File "untitled-1.py", line 19, in build
     return MySlider()
   File "site-packages\kivy\uix\widget.py", line 321, in __init__
   File "site-packages\kivy\lang\builder.py", line 447, in apply
   File "site-packages\kivy\lang\builder.py", line 502, in _apply_rule
   File "site-packages\kivy\lang\builder.py", line 827, in _build_canvas
 kivy.lang.builder.BuilderException: Parser: File "<inline>", line 7:
 ...
       5:            pos: (self.value_pos[0] - sp(16), self.center_y - sp(17)) i
f self.orientation == 'horizontal' else (self.center_x - (16), self.value_pos[1]
 - sp(16))
       6:            size: (sp(32), sp(32))
 >>    7:            source: 'rogue.jpg'
 ...
 Exception: SDL2: Unable to load image
   File "site-packages\kivy\lang\builder.py", line 822, in _build_canvas
   File "kivy\graphics\instructions.pyx", line 362, in kivy.graphics.instruction
s.VertexInstruction.source.__set__ (kivy\graphics\instructions.c:7177)
   File "kivy\graphics\context_instructions.pyx", line 381, in kivy.graphics.con
text_instructions.BindTexture.source.__set__ (kivy\graphics\context_instructions
.c:7309)
   File "site-packages\kivy\core\image\__init__.py", line 528, in __init__
   File "site-packages\kivy\core\image\__init__.py", line 716, in _set_filename
   File "site-packages\kivy\core\image\__init__.py", line 432, in load
   File "site-packages\kivy\core\image\__init__.py", line 200, in __init__
   File "site-packages\kivy\core\image\img_sdl2.py", line 41, in load

Failed to execute script untitled-1

我发现有趣的是,如果我从cwd中删除rogue.jpg图像,该程序工作得很好。它只给了我一个空白的白色矩形。 但为什么第一个案例出错而第二个案件没有呢?我该如何预防?

以下是image和源代码:

from kivy.app import App
from kivy.uix.slider import Slider
from kivy.lang import Builder

Builder.load_string('''
<Slider>:
    canvas:
        Rectangle:
            pos: (self.value_pos[0] - sp(16), self.center_y - sp(17)) if self.orientation == 'horizontal' else (self.center_x - (16), self.value_pos[1] - sp(16))
            size: (sp(32), sp(32))
            source: 'rogue.jpg'
''')

class MySlider(Slider):
    pass

class TestApp(App):
    def build(self):
        return MySlider()

TestApp().run()

可以找到用于Python 3.4,Kivy v1.9.2-dev0,Windows的已编译.exe文件here

1 个答案:

答案 0 :(得分:0)

您在图像源代码中缺少一个“sp”: - )

而不是:

pos: (self.value_pos[0] - sp(16), self.center_y - sp(17)) if self.orientation == 'horizontal' else (self.center_x - (16), self.value_pos[1] - sp(16))

试试这个:

pos: (self.value_pos[0] - sp(16), self.center_y - sp(17)) if self.orientation == 'horizontal' else (self.center_x - sp(16), self.value_pos[1] - sp(16))

或“反斜杠”格式化(至少此处不向右滚动太远只是为了看到):

pos: (self.value_pos[0] - sp(16), self.center_y - sp(17))\
    if self.orientation == 'horizontal'\
    else (self.center_x - sp(16), self.value_pos[1] - sp(16))