如何使用单独的 json 存储键作为带有 kivy 的按钮上的文本?

How can I use individual json storage keys for use as the text on a button with kivy?

我正在尝试进行文本输入,将其保存到 json 商店,然后使用 json 商店中的每个单独的键作为按钮上的文本。我已经阅读了所有我能想到的相关文档,我已经尝试搜索 google,并且我已经尝试在此处搜索。我试过索引键,但不支持。我真的想不通。这是我需要的最后一个主要功能运行。

这是我正在谈论的部分的 python 脚本。

class ScreenSix(Screen):

def Create_ShopList(self):
    store = JsonStore('user_meals.json')
    ingredients = store.get(self._name.text)['ingredients']
    return ingredients

def Meal_Options(self):
    store = JsonStore('users_meals.json')
    meals = store.keys()
    return meals

这是我的 kv 文件的相关部分。

<ScreenSix>:
name: 'shoplist'
id: shoplist

FloatLayout:
    canvas:
        Rectangle:
            pos: self.pos
            size: self.size
            source: '20.png'

    Label:
        text: 'Create Shopping List'
        font_size: 60
        pos_hint: {'center_x':.5,'center_y':.8}
        color: 0,0,0,1

    Label:
        text: 'Which meals would you like to add?'
        font_size: 40
        pos_hint: {'center_x':.5,'center_y':.7}
        color: 0,0,0,1

    Button:
        text: 'Menu'
        font_size: 36
        size_hint: .2,.1
        pos_hint: {'left':1,'y':0}
        on_release: app.root.current='menu'

    Button:
        text: 'Quit'
        font_size: 36
        size_hint: .2,.1
        pos_hint: {'right':1,'y':0}
        on_release: app.root.current='open'

    Button:
        text: ''
        font_size: 36
        size_hint: .2,.1
        pos_hint: {'center_x':.18,'center_y':.6}
        on_release: root.Create_ShopList()

    Button:
        text: ''
        font_size: 36
        size_hint: .2,.1
        pos_hint: {'center_x':.39,'center_y':.6}

    Button:
        text: ''
        font_size: 36
        size_hint: .2,.1
        pos_hint: {'center_x':.6,'center_y':.6}

    Button:
        text: ''
        font_size: 36
        size_hint: .2,.1
        pos_hint: {'center_x':.81,'center_y':.6}

我想让按钮文本成为在我的 json 存储中保存为键的名称。最后几个按钮是我希望显示餐点名称的按钮,我在尝试弄清楚时将第一个按钮用作测试按钮。我担心我可能做不到,如果是这样我不知道有什么好的选择。

您可以为此使用 RecycleView
我举了一个小例子。
试试这个:

from kivy.app import App
from kivy.lang import Builder
from kivy.uix.button import Button
from kivy.uix.recycleview import RecycleView

from kivy.storage.jsonstore import JsonStore


store = JsonStore("buttons.json")

store.put('button1', font_size="40sp", color=(1,.5,.5, 1))
store.put('button2', font_size="20sp", color=(.5,1, 1, 1))
store.put('button3', font_size="30sp", color=(1,.5,.5, 1))
store.put('button4', font_size="10sp", color=(.5,1, 1, 1))

for i in store: store[i]["text"] = i    

items = [store[i] for i in store]


class MyButton(Button):

    def print_data(self,data):
        print(data)


class MyRecycleView(RecycleView):

    def __init__(self, **kwargs):
        super(MyRecycleView,self).__init__(**kwargs)
        self.data = items
        self.refresh_from_data()


KV = '''

<MyButton>:
    on_release:
        root.print_data(self.text)

BoxLayout:
    orientation: 'vertical'
    MyRecycleView:
        id: rv
        viewclass: 'MyButton'
        RecycleBoxLayout:
            orientation: 'vertical'
            default_size: None, dp(56)
            default_size_hint: 1, None
            size_hint_y: None
            height: self.minimum_height

'''


class Test(App):
    def build(self):
        root = Builder.load_string(KV)
        return root


Test().run()