Skip to content

Instantly share code, notes, and snippets.

@AWolf81
Last active October 24, 2017 00:15
Show Gist options
  • Save AWolf81/3f122de3ef559eaec202 to your computer and use it in GitHub Desktop.
Save AWolf81/3f122de3ef559eaec202 to your computer and use it in GitHub Desktop.
Settingspage inside of a TabbedPanel
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
from kivy.properties import StringProperty
import json
settings_json = json.dumps([
{'type': "bool",
'title': 'A boolean value',
'desc': 'Bool 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': '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'
BoxLayout:
orientation: 'vertical'
Label:
text: 'Here will be the app.'
Label:
text: u'last mod value = {}'.format(root.last_mod)
# overriding settings styles
<-Settings>:
orientation: 'horizontal'
# canvas.before:
# Color:
# rgb: 0, 0, 0
# Rectangle:
# pos: self.pos
# size: self.size
<-SettingsPanel>:
spacing: 5
padding: 5
size_hint_y: None
height: self.minimum_height
state_image: 'atlas://data/images/defaulttheme/button'
''')
class DemoSettings(Widget):
last_mod = StringProperty('')
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()
#load ini
self.root_widget.last_mod = self.config.getdefault('example',
'last_mod', '')
return self.root_widget
def on_config_change(self, config, section, key, value):
print config, section, key, value
last_mod = '{0},{1}'.format(key, value)
self.root_widget.last_mod = last_mod
self.config.set(section, 'last_mod', last_mod)
self.config.write()
# 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',
'last_mod': 'last_mod'
})
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