Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何使用单个json存储键作为kivy按钮上的文本?_Python_Json_Kivy_Python 3.6_Jsonstore - Fatal编程技术网

Python 如何使用单个json存储键作为kivy按钮上的文本?

Python 如何使用单个json存储键作为kivy按钮上的文本?,python,json,kivy,python-3.6,jsonstore,Python,Json,Kivy,Python 3.6,Jsonstore,我试图获取文本输入,将其保存到jsonstore,然后将jsonstore中的每个键用作按钮上的文本。我已经阅读了我能想到的所有相关文档,我试着搜索谷歌,我试着在这里搜索。我尝试了索引键,但不支持。我真的搞不懂。这是我需要运行所有东西的最后一个主要功能 这是我所说的那部分的python脚本 class ScreenSix(Screen): def Create_ShopList(self): store = JsonStore('user_meals.json') ingred

我试图获取文本输入,将其保存到jsonstore,然后将jsonstore中的每个键用作按钮上的文本。我已经阅读了我能想到的所有相关文档,我试着搜索谷歌,我试着在这里搜索。我尝试了索引键,但不支持。我真的搞不懂。这是我需要运行所有东西的最后一个主要功能

这是我所说的那部分的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()

好的,这样可以得到按钮上的文本,但现在我似乎不知道如何按我想要的方式定位按钮。我想把它们放在屏幕中间,我似乎不能那样做。