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

2024-04-24 06:59:43 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试获取文本输入,将其保存到jsonstore,然后使用jsonstore中的每个键作为按钮上的文本。我已经阅读了我能想到的所有相关文档,我尝试过搜索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存储中的键的名称。最后几个按钮是我想让餐名显示在上面的按钮,我正在使用第一个按钮作为测试按钮,同时我正在尝试找出它。我担心我可能做不到,如果是这样的话,我不知道有什么好的选择。你知道吗


Tags: storetextpos文本selfjsonsizecreate
1条回答
网友
1楼 · 发布于 2024-04-24 06:59:43

你可以用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()

相关问题 更多 >