kivy如何自动保存和加载用户输入

问题描述 投票:0回答:2

我正在制作一个应用程序,其中有一个配置文件屏幕,您可以在其中使用文本输入框输入通用配置文件信息(姓名、身高、体重等)。我知道有一种方法可以在每个文本输入框旁边放一个按钮来保存信息,另一个按钮来加载信息。我想知道是否有一种方法可以在用户打开应用程序时自动加载此信息,而不是通过点击按钮手动加载信息。有人建议使用 ConfigParser 的子类来解析标准的 ini 文件并使用它来加载特定于应用程序的设置,但我不知道该怎么做。

Kivy 文件:

<Phone>:
result: _result
h: _h
w: _w


AnchorLayout:
    anchor_x: 'center'
    anchor_y: 'top'

    ScreenManager:
        size_hint: 1, .9
        id: _screen_manager
        Screen:
            name: 'home'
            canvas.before:
                Rectangle:
                    pos: self.pos
                    size: self.size
                    source: "/home/aaron/Desktop/main.png"
            Label:
                markup: True
                text: '[size=100][color=ff3333]Welcome to [color=ff3333]Diabetes Manager[/color][/size]'
        Screen:
            name: 'menu'
            GridLayout: 
                cols: 2
                padding: 50
                canvas.before:
                    Rectangle:
                        pos: self.pos
                        size: self.size
                        source: "/home/aaron/Desktop/main.png"

                Button:
                    text: 'My Profile'
                    on_press: _screen_manager.current = 'profile' 
                Button:
                    text: 'History'
                    on_press: _screen_manager.current = 'history'     

                Button:
                    text: 'New Entry'
                    on_press: _screen_manager.current = 'new_entry' 
                Button:
                    text: 'Graph'
                    on_press: _screen_manager.current = 'graph' 
                Button:
                    text: 'Diet'
                    on_press: _screen_manager.current = 'diet' 
                Button:
                    text: 'Settings'
                    on_press: _screen_manager.current = 'settings' 

        Screen:
            name: 'profile'
            GridLayout: 
                cols: 1
                BoxLayout:
                    Label:
                        size_hint_x: 0.22
                        bold: True
                        markup: True
                        text: '[size=40][color=0000ff]Name[/color][/size]'
                    TextInput:
                        id: _name
                        hint_text: 'Name'

                BoxLayout:
                    Label:  
                        size_hint_x: 0.22
                        bold: True
                        markup: True
                        text: '[size=40][color=0000ff]Gender[/color][/size]'
                    TextInput:
                        id: _gender1
                        hint_text: 'Gender'

                BoxLayout:
                    Label:
                        size_hint_x: 0.22
                        bold: True
                        markup: True
                        text: '[size=34][color=0000ff]Type of Diabetes[/color][/size]'
                    TextInput:
                        id: _type
                        hint_text: 'Type of Diabetes'

                BoxLayout:
                    Label:
                        size_hint_x: 0.22
                        bold: True
                        markup: True
                        text: '[size=40][color=0000ff]Height (in)[/color][/size]'
                    TextInput:
                        id: _h
                        hint_text: 'Height in inches'

                BoxLayout:
                    Label:
                        size_hint_x: 0.22
                        bold: True
                        markup: True
                        text: '[size=40][color=0000ff]Weight (lb)[/color][/size]'
                    TextInput:
                        id: _w
                        hint_text: 'Weight in pounds'

                BoxLayout:
                    Button:
                        text: 'Calculate BMI'
                        on_press: root.product(*args)

                    Label:
                        size_hint_x: 4.5
                        id:_result
                        bold: True
                        markup: True
                        text: '[size=40][color=0000ff]BMI[/color][/size]'

                BoxLayout:
                    Label:
                        size_hint_x: 0.22
                        bold: True
                        markup: True
                        text: '[size=30][color=0000ff]List of Medications[/color][/size]'
                    TextInput:
                        id: _meds
                        hint_text: 'List of Medications'

                BoxLayout:
                    Label:
                        size_hint_x: 0.22
                        bold: True
                        markup: True
                        text: '[size=38][color=0000ff]Insulin Times[/color][/size]'
                    TextInput:
                        id: _times
                        hint_text: 'Please Enter Times to Take Insulin'


        Screen:
            name: 'history'
            GridLayout: 
                cols:1

        Screen:
            name: 'new_entry'
            GridLayout:
                cols:1

                BoxLayout:
                    Label:
                        size_hint_x: 0.22
                        bold: True
                        markup: True
                        text: '[size=40][color=0000ff]Time[/color][/size]'
                    TextInput:
                        id: _time
                        hint_text: 'Current Time'

                BoxLayout:
                    Label:
                        size_hint_x: 0.22
                        bold: True
                        markup: True
                        text: '[size=28][color=0000ff]Blood Sugar (mg/dL)[/color][/size]'
                    TextInput:
                        id: _glucose_reading
                        hint_text: 'Current Blood Sugar'

                BoxLayout:
                    Label:
                        size_hint_x: 0.22
                        bold: True
                        markup: True
                        text: '[size=40][color=0000ff]Carbs[/color][/size]'
                    TextInput:
                        id: _food
                        hint_text: 'Total Carbs for meal'

                BoxLayout:
                    Label:
                        size_hint_x: 0.22
                        bold: True
                        markup: True
                        text: '[size=30][color=0000ff]Medications Taken[/color][/size]'
                    TextInput:
                        id: _meds_taken
                        hint_text: 'Please Enter Any Medications Taken'


        Screen:
            name: 'graph'
            GridLayout: 
                cols: 3
                padding: 50
            Label: 
                markup: True
                text: '[size=24][color=dd88ff]Your Graph[/color][/size]'

        Screen:
            name: 'diet'
            GridLayout: 
                cols: 3
                padding: 50
            Label: 
                markup: True
                text: '[size=24][color=dd88ff]Reccomended Diet[/color][/size]'


        Screen:
            name: 'settings'
            GridLayout: 
                cols: 3
                padding: 50
            Label: 
                markup: True
                text: '[size=24][color=dd88ff]Settings[/color][/size]'


AnchorLayout:
    anchor_x: 'center'
    anchor_y: 'bottom'
    BoxLayout:
        orientation: 'horizontal'
        size_hint: 1, .1
        Button:
            id: btnExit
            text: 'Exit'
            on_press: app.stop() 
        Button:
            text: 'Menu'
            on_press: _screen_manager.current = 'menu'
android python save load kivy
2个回答
1
投票

Kivy 内置存储功能。通过使用内置方法,它会将文件存储在适用于 Android 或 iOS 的正确位置,而您无需担心位置是否正确。这是他们的文档: https://kivy.org/docs/api-kivy.storage.html#module-kivy.storage

还有一个简短的例子来展示如何放置、检索和删除值:

from kivy.storage.jsonstore import JsonStore

store = JsonStore('baz.json')
store.put('foo', 'bar')

if store.exists('foo'):
    print('foo is:', store.get('foo'))
    store.delete('foo')

0
投票

问题是 jsonstore 太有限了,你不能使用一个文本输入添加更多信息,这意味着每当你想保存新信息时,它都会删除旧信息。所以 JsonStore 它用于注册新用户(登录和注册)或您不会或将要更改的内容并放置新信息

© www.soinside.com 2019 - 2024. All rights reserved.