Reputation: 167
I want to save the path of a image file obtained from user in a JSON file and change the image according to the path. The path is saved in the JSON file like this,
{"PathInfo": {"cover_path": "\"E:\\K75 Programming\\Python\\Mode\\Photos\\2.jpg\""}}
Therefore the image will not be changed. I want to save the path like this,
{"PathInfo": {"cover_path": "E:\K75 Programming\Python\Mode\Photos\2.jpg"}}
I think then the picture will be changed properly. So, how would I do that?
Here is my code
from kivy.lang.builder import Builder
from kivy.uix.screenmanager import Screen
from kivymd.app import MDApp
from kivy.storage.jsonstore import JsonStore
screen_helper = """
ScreenManager:
Studio:
EditCover:
<Studio>:
name : 'studio'
MDFloatLayout:
FitImage:
id: cover_pic
orientation: "vertical"
size_hint: 1, .75
pos_hint: {"center_x": .5, "center_y": 1}
source: "Photos/3.jpg"
<EditCover>:
name : 'edit_cover'
MDTextField:
id : cover_path
hint_text: "Path"
mode: "rectangle"
pos_hint: {'center_x':0.5,'center_y':0.5}
size_hint: (0.68,0.07)
MDRaisedButton:
text : 'OK'
pos_hint : {'center_x':0.9,'center_y':0.08}
font_style: 'Button'
ripple_rad_default : 40
ripple_duration_out : 1
md_bg_color: app.theme_cls.primary_dark
on_press : app.save_pic_path()
"""
class EditCover(Screen):
pass
class Studio(Screen):
pass
class Mode(MDApp):
def build(self):
return Builder.load_string(screen_helper)
def save_pic_path(self):
self.cover_path = self.root.get_screen('edit_cover').ids.cover_path.text
self.store_paths.put('PathInfo', cover_path = self.cover_path)
self.pic_changer()
def pic_changer(self):
self.root.get_screen('studio').ids.cover_pic.source = "{self.store_paths.get('PathInfo')['cover_path']}"
def on_start(self):
self.store_paths = JsonStore("paths.json")
try:
if self.store_paths.get('PathInfo')['cover_path'] != "":
self.pic_changer()
except KeyError as e:
self.root.get_screen('studio').ids.cover_pic.source = "Photos/3.jpg"
Mode().run()
However, I just want to get the path from user and change the image according to it.
Please kindly help me to complete this project.
Upvotes: 0
Views: 80
Reputation: 71
If you want to assign the cover_path value to pic source, I think you forgot to use the f-string notation as the following code:
def pic_changer(self):
self.root.get_screen('studio').ids.cover_pic.source = f"{self.store_paths.get('PathInfo')['cover_path']}"
I hope can help you
Upvotes: 1