Can't run "hello world" python code in kivy launcher for android

I am trying to run this code from kivy.org in kivy launcher on my link 5. I created a folder in the kivy folder and the "android.txt" file. Here's main.py:

import kivy
kivy.require('1.9.0')
from kivy.app import App
from kivy.uix.label import Label


class TestApp(App):
   def build(self):
          return Label(text='Hello World')
if __name__ == '__main__':
   TestApp().run()

      

When I run it from startup, it crashes instantly. And this is what I get in the logs folder:

[INFO              ] Logger: Record log in /storage/emulated/0/kivy/myshit/.kivy/logs/kivy_15-05-01_6.txt
[INFO              ] Kivy: v1.9.0
[INFO              ] Python: v2.7.2 (default, Apr  2 2015, 13:52:41) 
[GCC 4.8]
[INFO              ] Factory: 173 symbols loaded
[WARNING           ] stderr: /data/data/org.kivy.pygame/files/lib/python2.7/site-packages/kivy/core/image/img_pygame.py:1.3: RuntimeWarning: import cdrom: No module named cdrom
[WARNING           ] stderr: (ImportError: No module named cdrom)
[INFO              ] Image: Providers: img_tex, img_dds, img_gif, img_pygame, img_pil (img_ffpyplayer ignored)
[WARNING           ] stderr: Traceback (most recent call last):
[WARNING           ] stderr:   File "main.py", line 9, in <module>
[WARNING           ] stderr:     return Label(text='Hello World')
[WARNING           ] stderr:   File "/home/tito/code/python-for-android/build/python-install/lib/python2.7/site-packages/kivy/app.py", line 798, in run
[WARNING           ] stderr:   File "main.py", line 7, in build
[WARNING           ] stderr:     class TestApp(App):
[WARNING           ] stderr: NameError: global name 'Label' is not defined

      

Please tell me what am I doing wrong? Postscript I am not compiling the app, I just won it in the launcher. The game examples and showcases work great.

+3


source to share


1 answer


Try the following:

import kivy
kivy.require('1.9.0')
from kivy.app import App
from kivy.lang import Builder

kv = '''
Label:
    text: "Hello World"
'''

class TestApp(App):
   def build(self):
          return Builder.load_string(kv)
if __name__ == '__main__':
   TestApp().run()

      



I honestly don't know why you code didn't run, but I haven't seen anyone just immediately return a kivy widget (like Label

or Button

) from a build method before, so maybe it has something to do with this ... From what I've seen, usually the build method returns an instance of the class, which is the root widget of the application. This class often either inherits from the kiw layout or the Widget class itself, and encapsulates everything else like the root of the widget tree.

+1


source







All Articles