你能自动加载kivy用户信息吗?

时间:2016-03-28 19:40:53

标签: python save load kivy textinput

我正在创建一个应用程序,其中有一个配置文件屏幕,您可以使用textinput框输入通用配置文件信息(名称,高度,重量等)。我知道有一种方法可以在每个textinput框旁边放一个按钮来保存信息,另一个按钮可以加载信息。我想知道是否有办法在用户打开应用程序时自动加载此信息,而不是通过点击按钮手动加载信息。

<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'

3 个答案:

答案 0 :(得分:2)

您可以将信息保存在json(导入json)文件中,然后使用on_start事件方法加载它。

答案 1 :(得分:2)

Kivy使用subclass of ConfigParser来解析标准ini文件。有关如何使用此文件加载app-specific settings的文档位于kivy.app文档页面上。

来自文档:

class TestApp(App):
    def build_config(self, config):
        config.setdefaults('section1', {
            'key1': 'value1',
            'key2': '42'
        })

    def build(self):
        config = self.config
        return Label(text='key1 is %s and key2 is %d' % (
            config.get('section1', 'key1'),
            config.getint('section1', 'key2')))

答案 2 :(得分:1)

好吧,每个App都以build()函数开头,期望返回一个根小部件,因此您可以使用App类中的函数加载一个简单文件并推送值通过ids或通过root的小部件children

来访问每个小部件

或在您要更新的窗口小部件值的类__init__()内执行相同的加载功能。

例如class MyBox(BoxLayout)是一个包含子项的类,您想要更新它们的值。然后在MyBox.__init__()内调用加载函数。您可以进一步简化它:使用__init__()中的加载函数并创建一个列表/字典/变量,您将传递值。在kv文件中,您只需访问变量,例如root.<variable>