File: settings.py

package info (click to toggle)
kivy 2.3.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 35,316 kB
  • sloc: python: 80,678; ansic: 5,326; javascript: 780; objc: 725; lisp: 195; sh: 173; makefile: 150
file content (93 lines) | stat: -rw-r--r-- 3,445 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
from kivy.app import App
from kivy.uix.button import Button
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.label import Label
from kivy.uix.popup import Popup
from kivy.uix.settings import (SettingsWithSidebar,
                               SettingsWithSpinner,
                               SettingsWithTabbedPanel)
from kivy.properties import OptionProperty, ObjectProperty


class SettingsApp(App):

    display_type = OptionProperty('normal', options=['normal', 'popup'])

    settings_popup = ObjectProperty(None, allownone=True)

    def build(self):

        paneltype = Label(text='What kind of settings panel to use?')

        sidebar_button = Button(text='Sidebar')
        sidebar_button.bind(on_press=lambda j: self.set_settings_cls(
            SettingsWithSidebar))
        spinner_button = Button(text='Spinner')
        spinner_button.bind(on_press=lambda j: self.set_settings_cls(
            SettingsWithSpinner))
        tabbed_button = Button(text='TabbedPanel')
        tabbed_button.bind(on_press=lambda j: self.set_settings_cls(
            SettingsWithTabbedPanel))

        buttons = BoxLayout(orientation='horizontal')
        buttons.add_widget(sidebar_button)
        buttons.add_widget(spinner_button)
        buttons.add_widget(tabbed_button)

        displaytype = Label(text='How to display the settings?')
        display_buttons = BoxLayout(orientation='horizontal')
        onwin_button = Button(text='on window')
        onwin_button.bind(on_press=lambda j: self.set_display_type('normal'))
        popup_button = Button(text='in a popup')
        popup_button.bind(on_press=lambda j: self.set_display_type('popup'))
        display_buttons.add_widget(onwin_button)
        display_buttons.add_widget(popup_button)

        instruction = Label(text='Click to open the settings panel:')
        settings_button = Button(text='Open settings')
        settings_button.bind(on_press=self.open_settings)

        layout = BoxLayout(orientation='vertical')
        layout.add_widget(paneltype)
        layout.add_widget(buttons)
        layout.add_widget(displaytype)
        layout.add_widget(display_buttons)
        layout.add_widget(instruction)
        layout.add_widget(settings_button)

        return layout

    def on_settings_cls(self, *args):
        self.destroy_settings()

    def set_settings_cls(self, panel_type):
        self.settings_cls = panel_type

    def set_display_type(self, display_type):
        self.destroy_settings()
        self.display_type = display_type

    def display_settings(self, settings):
        if self.display_type == 'popup':
            p = self.settings_popup
            if p is None:
                self.settings_popup = p = Popup(content=settings,
                                                title='Settings',
                                                size_hint=(0.8, 0.8))
            if p.content is not settings:
                p.content = settings
            p.open()
        else:
            super(SettingsApp, self).display_settings(settings)

    def close_settings(self, *args):
        if self.display_type == 'popup':
            p = self.settings_popup
            if p is not None:
                p.dismiss()
        else:
            super(SettingsApp, self).close_settings()


if __name__ == '__main__':
    SettingsApp().run()