2016-03-31 11 views
1

Ich mache eine App, wo es einen Profilbildschirm gibt, in dem Sie generische Profilinformationen (Name, Höhe, Gewicht, usw.) mit Texteingabe-Boxen eingeben können. Ich weiß, dass es eine Möglichkeit gibt, neben jeder Texteingabe-Schaltfläche eine Schaltfläche zu platzieren, um die Information zu speichern, und eine andere Schaltfläche, um die Information zu laden. Ich frage mich, ob es eine Möglichkeit gibt, diese Informationen automatisch zu laden, wenn der Benutzer die App öffnet, anstatt die Informationen manuell durch Drücken einer Taste zu laden. Einige haben vorgeschlagen, eine Unterklasse von ConfigParser zu verwenden, um Standard-INI-Dateien zu analysieren und damit anwendungsspezifische Einstellungen zu laden, aber ich habe keine Ahnung, wie das geht.Wie Benutzereingaben automatisch speichern und laden kivy

Kivy Datei:

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

Mögliche Duplikat [Können Sie automatisch Benutzerinformationen in Kivy laden] (http: // Stackoverflow .com/questions/36269812/can-you-automatisch-load-user-information-in-kivy) – zeeMonkeez

+0

konnte meine letzte Frage nicht löschen und wollte meinen Code hinzufügen, bevor Leute anfingen zu antworten – Azaro

+0

Nur ein allgemeiner Kommentar: Es ist normalerweise besser ein neues Konzept mit einigen Spielzeugbeispielen als eine vollständige Anwendung zu verstehen. CF [fragen] und [MCVE]. Genauer gesagt: Die Dokumentation, die ich in meiner Antwort auf Ihre frühere Frage verlinkt habe, zeigt genau, wie man init config konfiguriert und wie man Konfigurationswerte liest, um ein 'Label' zu füllen. Ich schlage vor, Sie spielen mit diesem Beispiel, finden Sie heraus, wie Sie es anwenden. Wenn die Dinge noch unklar sind, stellen Sie eine spezifische Frage mit einem kleinen Beispiel. – zeeMonkeez

Antwort

1

Kivy hat eingebaute Speicherfunktionalität. Durch die Verwendung der integrierten Methoden wird die Datei an der richtigen Stelle für Android oder iOS gespeichert, ohne dass Sie sich um den korrekten Speicherort kümmern müssen. Hier ist ihre docs: https://kivy.org/docs/api-kivy.storage.html#module-kivy.storage

Und ein kurzes Beispiel zu zeigen, wie zu setzen, abrufen und löschen Werte:

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') 
Verwandte Themen