简体   繁体   中英

can't run “hello world” python code in kivy launcher for android

I'm trying to run this code from kivy.org in kivy launcher on my nexus 5. I've made a folder in kivy folder and "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 start it from launcher it crashes instantly.And this is what I get in 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 guide me what do I do wrong? PS I don't compile app, I just wonna run it in launcher. Sample games and showcase work fine.

Try this:

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're code wouldn't run, but I haven't seen someone just straight-up return a kivy widget(like Label or Button ) from the build method before, so perhaps it's something to do with that. From what I've seen, usually the build method returns an instance of a class that is the root widget of an app. That class often seems to either inherit from a kivy layout, or the Widget class itself, and encapsulates everything else, as the root of the widget tree.

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