android - How to automatically load data from json file on startup with kivy -
i making android app python , kivy keeps track of diabetes entries , has basic profile page. figured out how save input textinput widgets json file works fine. cannot figure out how automatically load information textinput widets on startup. loading labels work sloppier. know there on_start module, have no idea how use it. suggestions appreciated.
.kv file
<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 [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 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 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 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
kivy.app import app kivy.lang import builder kivy.uix.popup import popup kivy.uix.button import button kivy.graphics import color, rectangle kivy.uix.boxlayout import boxlayout kivy.uix.floatlayout import floatlayout kivy.uix.image import asyncimage kivy.uix.label import label kivy.properties import stringproperty, listproperty kivy.uix.behaviors import buttonbehavior kivy.uix.textinput import textinput kivy.network.urlrequest import urlrequest kivy.storage.jsonstore import jsonstore os.path import join os.path import exists kivy.compat import iteritems kivy.storage import abstractstore json import loads, dump kivy.config import config class phone(floatlayout): def __init__(self, **kwargs): # make sure aren't overriding important functionality super(phone, self).__init__(**kwargs) self.canvas.before: color(0, 1, 0, 1) # green; colors range 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()
you can reverse of you're doing in save()
. example:
def load(self): store = jsonstore('hello.json') profile = store.get('profile') self.n.text = profile['name'] self.g.text = profile['gender'] ...
then call method somewhere, in __init__()
.
Comments
Post a Comment