Advertisement
jevixlugya

Untitled

Oct 24th, 2022 (edited)
167
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.49 KB | Source Code | 0 0
  1. from kivy.app import App
  2. from kivy.lang import Builder
  3. # from kivy.storage.jsonstore import JsonStore  # removed,  no need to use JsonStore
  4. from kivy.uix.boxlayout import BoxLayout
  5. from plyer import filechooser
  6. import json
  7. from pathlib import Path
  8.  
  9. kv = """
  10. MainJson:
  11.    orientation: 'vertical'
  12.    padding:10
  13.    spacing:10
  14.    Label:
  15.        text: 'Example of Json'
  16.        size_hint_y: None
  17.        height: 30
  18.    ScrollView:
  19.        size:self.size
  20.        GridLayout:
  21.            cols:2
  22.            padding:10
  23.            spacing:10
  24.            height:self.minimum_height
  25.            width:self.minimum_width
  26.  
  27.            Button:
  28.                text:'upload photo'
  29.                on_press:root.filechooseropenfile()
  30.                background_color:1,1,0,1
  31.            Image:
  32.                source:''
  33.                id:image1    
  34.            Label:
  35.                text:'Name: '
  36.            TextInput:
  37.                multline:False
  38.                id:name
  39.                hint_text:'full name: '
  40.            Label:
  41.                text:'age'
  42.    
  43.            TextInput:
  44.                multline:False
  45.                id:age
  46.                hint_text:'your Age: '
  47.            Button:
  48.                text:'save'
  49.                background_color:0,0,1,1
  50.                on_press:root.save()
  51.            Button:
  52.                text:'Load'
  53.                background_color:0,1,0,1
  54.                on_press:root.load()
  55.    BoxLayout:
  56.        orientation:'vertical'
  57.        TextInput:
  58.            id:display
  59.            readonly:True
  60.        Image:
  61.            source:''
  62.            id:simg
  63.  
  64. """
  65. FILENAME = 'user_data.json'
  66.  
  67. class MainJson(BoxLayout):
  68.     def __init__(self, **kwargs):
  69.         super().__init__(**kwargs)
  70.         # self.store = JsonStore('storage.json') removed - not used
  71.         self.p = Path(FILENAME)
  72.  
  73.     def save(self):
  74.         dict1 = {'name': self.ids.name.text, 'age': self.ids.age.text, "image": self.ids.image1.source}
  75.         with open(self.p, 'w') as f:
  76.             #     f.write(json.dumps(dict1, indent=4))
  77.             json.dump(dict1, f, indent=4)
  78.  
  79.     def load(self):
  80.         if self.p.exists():
  81.             with open(self.p) as jn:
  82.                 d = json.load(jn)  # dict is a python builtin, I changed the name to d
  83.             self.ids.display.text = f"The saved data:  Name: {d['name']}, age: {d['age']}"
  84.             self.ids.simg.source=f"{d['image']}"
  85.  
  86.         else:
  87.             self.ids.display.text = 'file does not exist'
  88.     def filechooseropenfile(self):
  89.         filechooser.open_file(on_selection=self.selected)
  90.  
  91.     def selected(self,selection):
  92.    
  93.         if selection:
  94.             self .ids.image1.source=selection[0]    
  95.  
  96.     count=1
  97.  
  98. class JsonExampleApp(App):
  99.     def build(self):
  100.         return Builder.load_string(kv)
  101.  
  102.     def on_start(self):
  103.         # load the data if it is available
  104.         p = Path(FILENAME)
  105.         if p.exists():
  106.             with open(p) as jn:
  107.                 d = json.load(jn)
  108.             # check if the file contains both name and age, if not delete the file
  109.             if set([ 'age', 'name','image']) == d.keys():
  110.                 print('valid file, file contains the required keys')
  111.                 self.root.ids.name.text = d['name']
  112.                 self.root.ids.age.text = d['age']
  113.                 self.root.ids.age.text = d['image']
  114.             else:
  115.                 p.unlink()
  116.                 print('invalid file deleted')
  117.  
  118.  
  119. JsonExampleApp().run()
Advertisement
Comments
Add Comment
Please, Sign In to add comment
Advertisement