如何将从用户处获取的文件路径保存在Json文件中?
How to save the path of a file obtained from the user in a Json file?
我想把一个从用户那里得到的图片文件的路径保存在一个JSON文件中,然后根据路径更改图片。路径保存在JSON文件中是这样的,
{"PathInfo": {"cover_path": "\"E:\K75 Programming\Python\Mode\Photos\2.jpg\""}}
因此不会更改图像。我想这样保存路径,
{"PathInfo": {"cover_path": "E:\K75 Programming\Python\Mode\Photos.jpg"}}
我想这样图片就可以改正了。那么,我该怎么做呢?
这是我的代码
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()
但是,我只想从用户那里获取路径并根据它更改图像。
- 这个能按照我想的那样正确吗?
- 或者,有没有更好的方法正确地做到这一点?
请帮助我完成这个项目。
如果你想给图片源赋cover_path值,我想你忘了使用f-string表示法,如下代码:
def pic_changer(self):
self.root.get_screen('studio').ids.cover_pic.source = f"{self.store_paths.get('PathInfo')['cover_path']}"
希望能帮到你
我想把一个从用户那里得到的图片文件的路径保存在一个JSON文件中,然后根据路径更改图片。路径保存在JSON文件中是这样的,
{"PathInfo": {"cover_path": "\"E:\K75 Programming\Python\Mode\Photos\2.jpg\""}}
因此不会更改图像。我想这样保存路径,
{"PathInfo": {"cover_path": "E:\K75 Programming\Python\Mode\Photos.jpg"}}
我想这样图片就可以改正了。那么,我该怎么做呢?
这是我的代码
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()
但是,我只想从用户那里获取路径并根据它更改图像。
- 这个能按照我想的那样正确吗?
- 或者,有没有更好的方法正确地做到这一点?
请帮助我完成这个项目。
如果你想给图片源赋cover_path值,我想你忘了使用f-string表示法,如下代码:
def pic_changer(self):
self.root.get_screen('studio').ids.cover_pic.source = f"{self.store_paths.get('PathInfo')['cover_path']}"
希望能帮到你