【发布时间】:2020-09-08 10:22:25
【问题描述】:
我正在构建一个应用程序,并且想知道如何将 kivy 设置重置为其默认值。此外,是否可以添加一个将设置重置为其默认值的按钮。这是我的代码:
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.settings import SettingsWithSidebar
from plyer.facades.storagepath import StoragePath
import plyer, json
settings_json = json.dumps([
{'type': 'title',
'title': 'example title'},
{'type': 'bool',
'title': 'A boolean setting',
'desc': 'Boolean description text',
'section': 'example',
'key': 'boolexample'},
{'type': 'numeric',
'title': 'A numeric setting',
'desc': 'Numeric description text',
'section': 'example',
'key': 'numericexample'},
{'type': 'options',
'title': 'An options setting',
'desc': 'Options description text',
'section': 'example',
'key': 'optionsexample',
'options': ['option1', 'option2', 'option3']},
{'type': 'string',
'title': 'A string setting',
'desc': 'String description text',
'section': 'example',
'key': 'stringexample'},
{'type': 'path',
'title': 'Download Folder Path',
'desc': "All downloaded manga will be found in this folder called 'manga_downloader_root' ",
'section': 'example',
'key': 'pathexample'}])
Builder.load_string('''
<Interface>:
orientation: 'vertical'
Button:
text: 'open the settings!'
font_size: 150
on_release: app.open_settings()
on_press: app.destroy_settings()
''')
class Interface(BoxLayout):
pass
class SettingsApp(App):
def build(self):
self.settings_cls = SettingsWithSidebar
self.use_kivy_settings = False
setting = self.config.get('example', 'pathexample')
print(setting)
print(plyer.storagepath.get_downloads_dir())
return Interface()
def build_config(self, config):
user_downloads_dir = plyer.storagepath.get_downloads_dir()
config.setdefaults('example', {
'boolexample': True,
'numericexample': 10,
'optionsexample': 'option2',
'stringexample': 'some_string',
'pathexample': user_downloads_dir })
def build_settings(self, settings):
settings.add_json_panel('Panel Name', self.config, data=settings_json)
def on_config_change(self, config, section, key, value):
print (config, section, key, value)
if __name__ == "__main__":
SettingsApp().run()
关于更多上下文,我希望让这个应用程序在 android 和 PC 上都能运行。是否也可以将此代码合并到可以通过图标打开的导航栏中。
【问题讨论】: