如何在KivyMD中保存用户的输入(MAGE文件) [英] How to save user's input (mage file) in KivyMD
本文介绍了如何在KivyMD中保存用户的输入(MAGE文件)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我制作了一个简单的应用程序,它接受用户的输入(图像文件)。但每当它关闭时,输入的文件就会丢失(当然,我没有为保存它设置一个方法)。 请,我希望输入的图像文件是可用的,当下一次应用程序打开,所以请我如何保存输入的图像文件(Perharbs你可以给我一个示例代码,指示我在哪里或任何地方,我真的很感激)。非常感谢您的帮助。
以下是我的代码:
from kivy.lang import Builder
from kivymd.app import MDApp
from kivy.core.window import Window
from plyer import filechooser
Window.size = (300, 530)
KV = """
MDBoxLayout:
orientation: 'vertical'
MDToolbar:
id: progress_toolbar
title: 'Progress'
ScrollView:
MDGridLayout:
cols: 2
adaptive_height: True
spacing: (10, 15)
padding: [25, 25]
MDLabel:
halign: 'center'
text: 'Before'
MDLabel:
halign: 'center'
text: 'Now'
MDCard:
ripple_behavior: True
orientation: 'vertical'
size_hint_y: None
size: 120, 220
elevation: 15
radius: 8
MDIconButton:
icon: "camera-outline"
user_font_size: "24sp"
pos_hint: {"center_x": .5, "center_y": .5}
on_release: app.file_chooser1()
Image:
id: img1
allow_stretch: True
keep_ratio: False
# size_hint_y: .5
MDCard:
ripple_behavior: True
orientation: 'vertical'
size_hint_y: None
size: 120, 220
elevation: 15
radius: 8
MDIconButton:
icon: "camera-outline"
user_font_size: "24sp"
pos_hint: {"center_x": .5, "center_y": .5}
on_release: app.file_chooser2()
Image:
id: img2
allow_stretch: True
keep_ratio: False
# size_hint_y: .5
MDTextField:
hint_text: 'Date'
width: 100
MDTextField:
hint_text: 'Date'
width: 100
"""
class Example(MDApp):
def build(self):
return Builder.load_string(KV)
def file_chooser1(self):
filechooser.open_file(on_selection=self.selected1)
def file_chooser2(self):
filechooser.open_file(on_selection=self.selected2)
def selected1(self, selection1):
self.root.ids.img1.source = selection1[0]
def selected2(self, selection2):
self.root.ids.img2.source = selection2[0]
Example().run()
再次感谢您的帮助,我将不胜感激。
推荐答案
我认为您应该记录图像文件。
如何?
您可以将kivy.storage.jsonstore.JsonStore
类用于日志,就像数据库一样。
from kivy.storage.jsonstore import JsonStore
store = JsonStore('database.json')
...
def selected1(self, selection1):
self.root.ids.img1.source = selection1[0]
files_list = store['database']['files']
files_list.append(selection1[0])
store.put('database', files=files_list)
像这样。我希望我能帮到你。您可以搜索如何使用JsonStore Kivy?
这篇关于如何在KivyMD中保存用户的输入(MAGE文件)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文