创建Kivy应用程序时,我总是遇到此错误

时间:2016-07-15 07:55:29

标签: python kivy

我正在尝试在Windows上制作一个由Kivy制作的简单应用程序:

import kivy
from kivy.app import App
from kivy.uix.label import Label


class MyApp(App):

    def build(self):
        return Label(text='Hello world')


if __name__ == '__main__':
    MyApp().run()

(编辑)但每次我得到这个很长的错误;这就是全部:

[INFO              ] [Logger      ] Record log in  C:\Users\danie\.kivy\logs\kivy_16-07-15_5.txt
[INFO              ] [Kivy        ] v1.9.1
[INFO              ] [Python      ] v3.4.4 (v3.4.4:737efcadf5a6, Dec 20 2015, 20:20:57) [MSC v.1600 64 bit (AMD64)]
[INFO              ] [Factory     ] 179 symbols loaded
[INFO              ] [Image       ] Providers: img_tex, img_dds, img_gif, img_sdl2, img_pil (img_ffpyplayer ignored)
[INFO              ] [Text        ] Provider: sdl2
[INFO              ] [OSC         ] using <thread> for socket
[INFO              ] [Window      ] Provider: sdl2
[INFO              ] [GL          ] GLEW initialization succeeded
[INFO              ] [GL          ] OpenGL version <b'4.4.0 - Build  10.18.15.4271'>
[INFO              ] [GL          ] OpenGL vendor <b'Intel'>
[INFO              ] [GL          ] OpenGL renderer <b'Intel(R) HD Graphics 530'>
[INFO              ] [GL          ] OpenGL parsed version: 4, 4
[INFO              ] [GL          ] Shading version <b'4.40 - Build 10.18.15.4271'>
[INFO              ] [GL          ] Texture max size <16384>
[INFO              ] [GL          ] Texture max units <32>
[INFO              ] [Shader      ] fragment shader: <b"WARNING: 0:6: '' :  #version directive missing">
[INFO              ] [Shader      ] vertex shader: <b"WARNING: 0:6: '' :  #version directive missing">
[WARNING           ] [Image       ] Unable to load image <C:\Python34\lib\site-packages\kivy\data\glsl\default.png>
[CRITICAL          ] [Window      ] Unable to find any valuable Window provider at all!
sdl2 - Exception: SDL2: Unable to load image
  File "C:\Python34\lib\site-packages\kivy\core\__init__.py", line 67, in core_select_lib
     cls = cls()
  File "C:\Python34\lib\site-packages\kivy\core\window\window_sdl2.py", line 138, in __init__
    super(WindowSDL, self).__init__()
  File "C:\Python34\lib\site-packages\kivy\core\window\__init__.py", line 722, in __init__
    self.create_window()
  File "C:\Python34\lib\site-packages\kivy\core\window\window_sdl2.py", line 255, in create_window
    super(WindowSDL, self).create_window()
  File "C:\Python34\lib\site-packages\kivy\core\window\__init__.py", line 897, in create_window
    self.render_context = RenderContext()
  File "kivy\graphics\instructions.pyx", line 756, in kivy.graphics.instructions.RenderContext.__init__ (kivy\graphics\instructions.c:10729)
  File "C:\Python34\lib\site-packages\kivy\core\image\__init__.py", line 512, in __init__
    self.filename = arg
  File "C:\Python34\lib\site-packages\kivy\core\image\__init__.py", line 700, in _set_filename
    mipmap=self._mipmap, nocache=self._nocache)
  File "C:\Python34\lib\site-packages\kivy\core\image\__init__.py", line 430, in load
    im = loader(filename, **kwargs)
  File "C:\Python34\lib\site-packages\kivy\core\image\__init__.py", line 198, in __init__
    self._data = self.load(filename)
  File "C:\Python34\lib\site-packages\kivy\core\image\img_sdl2.py", line 42, in load
    raise Exception('SDL2: Unable to load image')

[CRITICAL          ] [App         ] Unable to get a Window, abort.
 Exception ignored in: 'kivy.properties.dpi2px'
 Traceback (most recent call last):
   File "C:\Python34\lib\site-packages\kivy\utils.py", line 513, in __get__
     retval = self.func(inst)
   File "C:\Python34\lib\site-packages\kivy\metrics.py", line 175, in dpi
     EventLoop.ensure_window()
   File "C:\Python34\lib\site-packages\kivy\base.py", line 126, in ensure_window
     sys.exit(1)
 SystemExit: 1
[CRITICAL          ] [App         ] Unable to get a Window, abort.

我无法理解为什么会出现这个错误,你可以帮我解决这个错误吗?

2 个答案:

答案 0 :(得分:1)

虽然安装说明gstreamer是可选的,但我必须安装它才能让kivy工作(windows)。如在安装页面上:
python -m pip install kivy.deps.gstreamer --extra-index-url https://kivy.org/downloads/packages/simple/

(卸载再现上面的错误,再次安装它再修好)

答案 1 :(得分:1)

gstreamer的建议在我的情况下是误导性的,因为它似乎在我使用的1.9.1版本之前修复了。该错误记录在文档here的一个模糊的传递中:

  

如果没有使用wheel方法安装kivy,这些命令将不起作用,例如kivy.deps.sdl2将无法导入。相反,必须找到这些dll的位置,并以与示例类似的方式手动将它们传递给Tree类。

基本上它是Windows上的二进制/ dll问题,因为我没有使用“wheel方法”(也就是我使用了virtualenv并完成了我的pip安装)。我已经修好了这个方法:

  1. 在Windows上找到python共享文件夹。例如。在virtualenv中,您将在X:\ virtualentpath \ share
  2. 中找到它
  3. 复制glew和sdl2目录
  4. 完成step 2 here之后,在第3步之前,在dist文件夹中创建 share 文件夹并粘贴 glew sdl 目录
  5. 将库路径添加到myappname.spec文件

    coll = COLLECT(exe,
    Tree('dist\\share\\glew\\bin\\'),
    Tree('dist\\share\\sdl2\\bin\\'),
    Tree('c:\\Users\\myuser\\PycharmProjects\\myappname\\'),
           a.binaries,
           a.zipfiles,
           a.datas,
           strip=False,
           upx=True,
           name='myappname')
    
  6. 如果你需要gstreamer,你必须添加它,如果你不需要你没有。这很重要,因为唯一的gstreamer库比基本的两个大44倍:

    • glew + sdl = 7.31MB
    • gstreamer = 323MB

    我不知道这是不是正确的方法,但是在几个小时内我偶然发现了一个红色的鲱鱼之后,这是唯一的方法在我的情况下有用。

    重写Kivy文档中的最后一段将有助于Windows上的这种情况。