Kivy,如何在 canvas 中使用数据

Kivy, how to use datas in canvas

我有一个产品列表,我想用标签显示它。 我知道如何从 TextInput 获取数据,但现在我想在 canvas.

中显示我的列表

你怎么做到的?

编辑:

我在 'global' 中有这样的数据:

products = [{'name' : 'Coca' , 'price' : 1.8000},{'name' : 'Fanta' ,
'price' : 1.8000}]

我想在 GridLayout 中显示 2 列,x 行,2 个标签,1 个用于名称,另一个用于芬达、古柯等的价格 ...

<CommandeScreen>:
    GridLayout:
        rows: 4
        cols: 2
        Button:
            text: 'Coca'
        Label:
            text: '1.80 euros'
        Button:
            text: 'Orangina'
        Label:
            text: '1.80 euros'
        Button:
            text: 'Ajouter'
        Button:
            text: 'Quitter'
            on_press: root.manager.current = 'menu'

一种方法是将其添加到 class 定义中的 Python 代码中:

from kivy.uix.label import Label

class CommandeScreen(Screen):
    def __init__(self, **kwargs):
        super(CommandeScreen, self).__init__(**kwargs)
        products = [{'name' : 'Coca' , 'price' : 1.8000},{'name' : 'Fanta' ,
'price' : 1.8000}]
        for p in products:
            self.gl.add_widget(Label(text=p['name']))
            self.gl.add_widget(Label(text=str(p['price']))

在kv中,会是这样的:

<CommandeScreen>:
    gl: GL
    GridLayout:
        rows: 2
        GridLayout:
            id: GL
            cols: 2
            Label:
                text: 'Prix : '
            Label:
                text: 'In euros'

        GridLayout:
            cols: 2
            Button:
                text: 'Ajouter'
            Button:
                text: 'Quitter'
                on_press: root.manager.current = 'menu'

结果将是: