setting.py
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.settings import SettingsWithSidebar
import 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': 'A 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': 'A path setting',
'desc': 'Path description text', 'section': 'example',
'key': 'pathexample'},
])
Builder.load_string('''
<Interface>:
orientation: 'vertical'
Button:
text: 'open the settings!'
font_size: 150
on_release: app.open_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', 'boolexample')
print(setting)
return Interface()
def build_config(self, config):
config.setdefaults('example',{
'boolexample': True,
'numericexample': 10,
'optionsexample': 'option2',
'stringexample': 'some_string',
'pathexample': ''
})
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)
SettingsApp().run()
---------------
#logs
<kivy.config.ConfigParser object at 0x0000018DC6333FD0> example boolexample 1
<kivy.config.ConfigParser object at 0x0000018DC6333FD0> example boolexample 0
<kivy.config.ConfigParser object at 0x0000018DC6333FD0> example numericexample 100
<kivy.config.ConfigParser object at 0x0000018DC6333FD0> example optionsexample option3
<kivy.config.ConfigParser object at 0x0000018DC6333FD0> example optionsexample option1
<kivy.config.ConfigParser object at 0x0000018DC6333FD0> example stringexample hello
<kivy.config.ConfigParser object at 0x0000018DC6333FD0> example stringexample hi
reference:
No comments:
Post a Comment