如何使用kivy在启动时自动从json文件加载数据

时间:2016-04-19 20:46:49

标签: android python json load kivy

我正在使用python和kivy创建一个能够跟踪糖尿病条目的Android应用程序,并且有一个基本的个人资料页面。我想出了如何将textinput小部件的输入保存到一个工作正常的json文件中。但是我无法弄清楚如何在启动时自动将此信息加载到textinput widets中。加载到标签也可以工作,但它会更加邋..我知道有一个on_start模块,但我不知道如何使用它。任何建议都将不胜感激。

.kv文件

<Phone>:
result: _result
h: _h
w: _w
n: name_input
g: gender_input
t: _type
ti: _times
m: _meds





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_input
                        hint_text: 'Name'
                    Button:
                        size_hint_x: 0.15
                        text: 'Load'
                        on_press: root.load()



                BoxLayout:
                    Label:  
                        size_hint_x: 0.22
                        bold: True
                        markup: True
                        text: '[size=40][color=0000ff]Gender[/color][/size]'
                    TextInput:
                        id: gender_input
                        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'

                    Button:
                        size_hint_x: 0.15
                        text: 'Done'
                        on_press: root.save()


        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.save(_name.text, gender.txt)
        Button:
            text: 'Menu'
            on_press: _screen_manager.current = 'menu'

的.py

    from kivy.app import App
    from kivy.lang import Builder
    from kivy.uix.popup import Popup
    from kivy.uix.button import Button
    from kivy.graphics import Color, Rectangle
    from kivy.uix.boxlayout import BoxLayout
    from kivy.uix.floatlayout import FloatLayout
    from kivy.uix.image import AsyncImage
    from kivy.uix.label import Label
    from kivy.properties import StringProperty, ListProperty
    from kivy.uix.behaviors import ButtonBehavior
    from kivy.uix.textinput import TextInput
    from kivy.network.urlrequest import UrlRequest
    from kivy.storage.jsonstore import JsonStore
    from os.path import join
    from os.path import exists
    from kivy.compat import iteritems
    from kivy.storage import AbstractStore
    from json import loads, dump
    from kivy.config import Config



class Phone(FloatLayout):
    def __init__(self, **kwargs):
        # make sure we aren't overriding any important functionality
        super(Phone, self).__init__(**kwargs)

    with self.canvas.before:
        Color(0, 1, 0, 1)  # green; colors range from 0-1 instead of 0-255
        self.rect = Rectangle(size=self.size, pos=self.pos)

    self.bind(size=self._update_rect, pos=self._update_rect)

def _update_rect(self, instance, value):
    self.rect.pos = instance.pos
    self.rect.size = instance.size

def product(self, instance):
    self.result.text = str(float(self.w.text) * 703/ (float(self.h.text) * float(self.h.text)))

def save(self):
    store = JsonStore('hello.json')
    name = self.n.text
    gender = self.g.text
    dtype = self.t.text
    height = self.h.text
    weight = self.w.text
    medications = self.m.text
    store.put('profile', name=name, gender=gender, dtpe=dtype, height=height, weight=weight, medications=medications)


presentation = Builder.load_file("main.kv")

class PhoneApp(App):
    def build(self):
        store = JsonStore('hello.json')

        return Phone()



if __name__ == '__main__':
   PhoneApp().run()

1 个答案:

答案 0 :(得分:0)

您可以在save()中执行相反的操作。例如:

def load(self):
    store = JsonStore('hello.json')
    profile = store.get('profile')
    self.n.text = profile['name']
    self.g.text = profile['gender']
    ...

然后在某个地方调用该方法,例如__init__()