简体   繁体   中英

Python Kivy won't use SDL2, insists on using pygame

I struggle to make Kivy (1.11.0) use SDL2 on Ubuntu 18.04 desktop. It keeps demanding pygame but that's been deprecated and I don't want to use it for a new project.

On a fresh new Ubuntu 18.04 VM this is what I did:

~ $ sudo apt install libsdl2-dev libsdl2-image-dev mesa-common-dev python3-pip python3-venv
~ $ pip3 install --user poetry
~ $ poetry new kivytest
~ $ cd kivytest

~/kivytest $ poetry add kivy pillow
Creating virtualenv kivytest-sUhjZQq9-py3.6 in ~/.cache/pypoetry/virtualenvs
Using version ^1.11.1 for kivy
Using version ^7.0.0 for pillow

Updating dependencies
Resolving dependencies... (2.2s)

Writing lock file


Package operations: 20 installs, 0 updates, 0 removals

  - Installing certifi (2019.11.28)
  - [...]
  - Installing pillow (7.0.0)
  - Installing kivy (1.11.1)

With kivy installed I create a simple test.py file:

from kivy.app import App
from kivy.uix.button import Button

class TestApp(App):
    def build(self):
        return Button(text='Hello World')

TestApp().run()

However when I run it it fails:

~/kivytest $ export KIVY_GL_BACKEND="sdl2"

~/kivytest $ poetry run python3 ./test.py
[INFO   ] [Logger      ] Record log in ~/.kivy/logs/kivy_20-03-29_6.txt
[INFO   ] [Kivy        ] v1.11.1
[INFO   ] [Kivy        ] Installed at "~/.cache/pypoetry/virtualenvs/kivytest-sUhjZQq9-py3.6/lib/python3.6/site-packages/kivy/__init__.py"
[INFO   ] [Python      ] v3.6.9 (default, Nov  7 2019, 10:44:02) 
[GCC 8.3.0]
[INFO   ] [Python      ] Interpreter at "~/.cache/pypoetry/virtualenvs/kivytest-sUhjZQq9-py3.6/bin/python3"
[INFO   ] [Factory     ] 184 symbols loaded
[INFO   ] [Image       ] Providers: img_tex, img_dds, img_pil, img_gif (img_pygame, img_ffpyplayer ignored)
[INFO   ] [Text        ] Provider: pil(['text_pygame'] ignored)
[CRITICAL] [Window      ] Unable to find any valuable Window provider. Please enable debug logging (e.g. add -d if running from the command line, or change the log level in the config) and re-run your app to identify potential causes
egl_rpi - ImportError: cannot import name 'bcm'
  File "~/.cache/pypoetry/virtualenvs/kivytest-sUhjZQq9-py3.6/lib/python3.6/site-packages/kivy/core/__init__.py", line 63, in core_select_lib
    fromlist=[modulename], level=0)
  File ".../kivy/core/window/window_egl_rpi.py", line 12, in <module>
    from kivy.lib.vidcore_lite import bcm, egl

pygame - ModuleNotFoundError: No module named 'pygame'
  File ".../kivy/core/__init__.py", line 63, in core_select_lib
    fromlist=[modulename], level=0)
  File ".../kivy/core/window/window_pygame.py", line 13, in <module>
    import pygame

x11 - ModuleNotFoundError: No module named 'kivy.core.window.window_x11'
  File ".../kivy/core/__init__.py", line 63, in core_select_lib
    fromlist=[modulename], level=0)

[CRITICAL] [App         ] Unable to get a Window, abort.

How can I force it to use SDL2 ? I've got the system SDL2 libs installed, installed kivy after , etc.

This is not a duplicate of similar questions asking about Windows or Mac OS-X as I'm running it on Ubuntu and the solutions don't apply.

Thanks!

Ok, I found the reason - a few more SDL2 libraries are required in order to build kivy with SDL2 support. This works:

~ $ sudo apt install mesa-common-dev \
                     libsdl2-dev libsdl2-image-dev \
                     libsdl2-ttf-dev libsdl2-mixer-dev

And then remove and reinstall kivy to get it rebuilt with SDL2 support:

~/testkivy $ poetry remove kivy && poetry add kivy

Or with pip if you don't use poetry :

(testkivy-venv) ~ $ pip uninstall kivy && pip install --user kivy

Then it works:

$ ./test.py
... 
[INFO   ] [Image       ] Providers: img_tex, img_dds, img_sdl2, img_pil, img_gif (img_ffpyplayer ignored)
[INFO   ] [Text        ] Provider: sdl2(['text_pango'] ignored)
[INFO   ] [Window      ] Provider: sdl2(['window_egl_rpi'] ignored)
[INFO   ] [GL          ] Using the "OpenGL" graphics system
[INFO   ] [GL          ] Backend used <sdl2>                         <<<< YAY!!!
[INFO   ] [GL          ] OpenGL version <b'3.0 Mesa 19.2.8'>
[INFO   ] [GL          ] OpenGL vendor <b'Intel Open Source Technology Center'>
[INFO   ] [GL          ] OpenGL renderer <b'Mesa DRI Intel(R) HD Graphics 520 (Skylake GT2) '>
[INFO   ] [GL          ] OpenGL parsed version: 3, 0
[INFO   ] [GL          ] Shading version <b'1.30'>
[INFO   ] [GL          ] Texture max size <16384>
[INFO   ] [GL          ] Texture max units <32>
[INFO   ] [Window      ] auto add sdl2 input provider
...

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