面对在Python Kivy中添加数组到列表视图的问题

时间:2017-03-18 21:09:15

标签: python kivy

您能否请一看这个演示,让我知道为什么我无法将features = ["A","B","C"]加载到MyListView

from kivy.uix.boxlayout import BoxLayout
from kivy.lang import Builder
from kivy.base import runTouchApp

features = ["A","B","C"]
Builder.load_string("""
<MyListView>:
    ListView:
        item_strings: [str(index) for index in features]
""")

class MyListView(BoxLayout):
    pass

if __name__ == '__main__':
    runTouchApp(MyListView())

我收到以下错误

  

消息文件名称行位置追溯
           21个
       init C:\ Python27 \ ArcGIS10.4 \ lib \ site-packages \ kivy \ uix \ boxlayout.py 102
       init C:\ Python27 \ ArcGIS10.4 \ lib \ site-packages \ kivy \ uix \ layout.py 72
       init C:\ Python27 \ ArcGIS10.4 \ lib \ site-packages \ kivy \ uix \ widget.py 320
      申请C:\ Python27 \ ArcGIS10.4 \ lib \ site-packages \ kivy \ lang.py 1970
      _apply_rule C:\ Python27 \ ArcGIS10.4 \ lib \ site-packages \ kivy \ lang.py 2130
  “BuilderException:Parser:File”“”“,第4行:......         2 ::         3:ListView:

     
    
      

4:item_strings:[str(index)for features in features] ... BuilderException:Parser:File“”“”,第4行:...             2 ::             3:ListView:          4:item_strings:[str(index)for features in features] ... NameError:name'features'未定义File       “”C:\ Python27 \ ArcGIS10.4 \ lib \ site-packages \ kivy \ lang.py“”,第1742行,       在create_handler中           return eval(value,idmap),bound_list文件“”“”,第4行,在

中     
  
     

文件“”C:\ Python27 \ ArcGIS10.4 \ lib \ site-packages \ kivy \ lang.py“”,行   2115,在_apply_rule中       rctx ['ids'])文件“”C:\ Python27 \ ArcGIS10.4 \ lib \ site-packages \ kivy \ lang.py“”,第1747行,   在create_handler中       cause = tb)“

1 个答案:

答案 0 :(得分:1)

也许你的意思是:

from kivy.uix.boxlayout import BoxLayout
from kivy.lang import Builder
from kivy.base import runTouchApp


Builder.load_string("""
<MyListView>:
    ListView:
        item_strings: [str(index) for index in root.features]
""")


class MyListView(BoxLayout):
    features = ["A", "B", "C"]


if __name__ == '__main__':
    runTouchApp(MyListView())

如果您以前没有,我建议您阅读this