Skip to content

Instantly share code, notes, and snippets.

@aflp91
Forked from AWolf81/DemoSettingsTPanel.py
Created June 15, 2014 18:55
Show Gist options
  • Save aflp91/50419fd8b91f8b0f6422 to your computer and use it in GitHub Desktop.
Save aflp91/50419fd8b91f8b0f6422 to your computer and use it in GitHub Desktop.
from kivy.config import Config
Config.set('graphics', 'width', '320')
Config.set('graphics', 'height', '480')
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.widget import Widget
from kivy.uix.settings import SettingsWithNoMenu
import json
settings_json = json.dumps([
{'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': 'A path setting',
'desc': 'Path description text',
'section': 'example',
'key': 'pathexample'}
])
Builder.load_string('''
<DemoSettings>:
TabbedPanel:
default_tab: _app_tab
size: root.size
TabbedPanelItem:
id: _settings_tab
text: 'Settings'
TabbedPanelItem:
id: _app_tab
text: 'Application'
Label:
text: 'Here will be the app.'
<-SettingsPanel>:
spacing: 5
padding: 5
size_hint_y: None
height: self.minimum_height
''')
class DemoSettings(Widget):
pass
class DemoSettingsApp(App):
'''
Use settings in a TabbedPanel
'''
def display_settings(self, settings):
'''
Overriding display_settings to show setting in TabbedPanel.
Comment from display_settings base:
Display the settings panel. By default, the panel is drawn directly
on top of the window. You can define other behaviour by overriding
this method, such as adding it to a ScreenManager or Popup.
You should return True if the display is successful, otherwise False.
:param settings: A :class:`~kivy.uix.settings.Settings`
instance. You should define how to display it.
:type config: :class:`~kivy.uix.settings.Settings`
'''
if self.root_widget.ids._settings_tab.content is not settings:
self.root_widget.ids._settings_tab.add_widget(settings)
return True
return False
def build(self):
# Configure settings
self.use_kivy_settings = False
self.settings_cls = SettingsWithNoMenu
self.root_widget = DemoSettings()
self.open_settings()
return self.root_widget
def on_config_change(self, config, section, key, value):
print config, section, key, value
# demo settings panel from Alexander Taylor
def build_config(self, config):
config.setdefaults('example', {
'boolexample': True,
'numericexample': 10,
'optionsexample': 'option2',
'stringexample': 'some_string',
'pathexample': '/some/path'
})
def build_settings(self, settings):
settings.add_json_panel('Settings',
self.config,
data=settings_json)
# pausing / resume handlers
def on_pause(self):
# Here you can save data if needed
return True
def on_resume(self):
# Here you can check if any data needs replacing (usually nothing)
pass
if __name__ == '__main__':
DemoSettingsApp().run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment