I always get this error when creating a Kivy app

泪湿孤枕 提交于 2020-05-14 01:24:08

问题


I am trying to make a simple app made from Kivy on Windows:

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

(EDIT) But each time I get this very long error; this is all of it:

[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.

I can't understand why this error is appearing, can you help me out of this?


回答1:


Though the installation instructions say gstreamer is optional I had to install it to get kivy to work (windows). As on the installation page:
python -m pip install kivy.deps.gstreamer --extra-index-url https://kivy.org/downloads/packages/simple/

(uninstalling reproduced the error above, installing it again fixed it again)




回答2:


The gstreamer suggestion was misleading in my case, since it seems it was fixed before 1.9.1 version I'm using. The error is documented in a little obscure pass of the documentation here:

If kivy was not installed using the wheels method these commands will not work and e.g. kivy.deps.sdl2 will fail to import. Instead, one must find the location of these dlls and manually pass them to the Tree class in a similar fashion as the example.

Basically it's a binary / dll issue on Windows caused by the fact I'm not using the "wheels method" (aka I've used a virtualenv and done my pip install there). I've fixed this way:

  1. Locate the python share folder on windows. E.g. in a virtualenv you will find it on X:\virtualentpath\share
  2. Copy the glew and sdl2 directory
  3. After you've done step 2 here and before step 3, in the dist folder create a share folder and paste glew and sdl directories in it
  4. Add the libraries path to the myappname.spec file

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

If you need gstreamer you have to add it, if you don't need you haven't. It's important because the only gstreamer library is something like 44 times larger than the basic two:

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

I don't know if it's the right method but after I stumbled on a red herring after another in several hours this is the only method that works in my case.

Rewriting that last paragraph in the Kivy documentation will help in this cases on Windows.



来源:https://stackoverflow.com/questions/38390789/i-always-get-this-error-when-creating-a-kivy-app

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!