Created
November 28, 2021 17:51
-
-
Save AWolf81/f3f03e4478ec713381678a06f20be74c to your computer and use it in GitHub Desktop.
Kivy settings example with color picker option
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Config Example | |
============== | |
This file contains a simple example of how the use the Kivy settings classes in | |
a real app. It allows the user to change the caption, font_size of the label and | |
application background and stores these changes. | |
When the user next runs the programs, their changes are restored. | |
""" | |
from kivy.app import App | |
from kivy.uix.settings import SettingsWithTabbedPanel | |
from kivy.core.window import Window | |
from kivy.utils import get_color_from_hex | |
from kivy.graphics import Color | |
from kivy.logger import Logger | |
from kivy.lang import Builder | |
# Important! Use latest master version until the color feature will be available | |
# Once 2.1.0 released - uncommenting the following two lines possible | |
# import kivy | |
# kivy.require('2.1.0') | |
# We first define our GUI | |
kv = ''' | |
BoxLayout: | |
orientation: 'vertical' | |
Button: | |
text: 'Configure app (or press F1)' | |
on_release: app.open_settings() | |
Label: | |
id: label | |
text: 'Hello' | |
''' | |
# This JSON defines entries we want to appear in our App configuration screen | |
json = ''' | |
[ | |
{ | |
"type": "string", | |
"title": "Label caption", | |
"desc": "Choose the text that appears in the label", | |
"section": "App", | |
"key": "text" | |
}, | |
{ | |
"type": "numeric", | |
"title": "Label font size", | |
"desc": "Choose the font size the label", | |
"section": "App", | |
"key": "font_size" | |
}, | |
{ | |
"type": "color", | |
"title": "App background color", | |
"desc": "Choose the background color of the app", | |
"section": "App", | |
"key": "bg_color" | |
} | |
] | |
''' | |
class MyApp(App): | |
def build(self): | |
""" | |
Build and return the root widget. | |
""" | |
# The line below is optional. You could leave it out or use one of the | |
# standard options, such as SettingsWithSidebar, SettingsWithSpinner | |
# etc. | |
# self.settings_cls = MySettingsWithTabbedPanel | |
# We apply the saved configuration settings or the defaults | |
root = Builder.load_string(kv) | |
label = root.ids.label | |
label.text = self.config.get('App', 'text') | |
label.font_size = float(self.config.get('App', 'font_size')) | |
return root | |
def on_start(self): | |
# Initialize the background color | |
self.apply_bg_color(self.config.get('App', 'bg_color')) | |
def build_config(self, config): | |
""" | |
Set the default values for the configs sections. | |
""" | |
config.setdefaults('App', {'text': 'Hello', 'font_size': 20, 'bg_color': '000000'}) | |
def build_settings(self, settings): | |
""" | |
Add our custom section to the default configuration object. | |
""" | |
# We use the string defined above for our JSON, but it could also be | |
# loaded from a file as follows: | |
# settings.add_json_panel('App', self.config, 'settings.json') | |
settings.add_json_panel('App', self.config, data=json) | |
def setFontColor(self, bg_value): | |
# based on https://stackoverflow.com/a/1855903/1483981 | |
bgcolor = Color(*get_color_from_hex(bg_value)) | |
# Counting the perceptive luminance | |
# human eye favors green color... | |
a = 1 - (0.299 * bgcolor.r + 0.587 * bgcolor.g + 0.114 * bgcolor.b) | |
if a < 0.5: | |
# light color --> new text color = black | |
color = (0,0,0,1) | |
else: | |
color = (1,1,1,1) | |
self.root.ids.label.color = color | |
def apply_bg_color(self, value): | |
Window.clearcolor = value | |
# calculate font color - white or black based on contrast | |
self.setFontColor(value) | |
def on_config_change(self, config, section, key, value): | |
""" | |
Respond to changes in the configuration. | |
""" | |
Logger.info("main.py: App.on_config_change: {0}, {1}, {2}, {3}".format( | |
config, section, key, value)) | |
if section == "App": | |
if key == "text": | |
self.root.ids.label.text = value | |
elif key == 'font_size': | |
self.root.ids.label.font_size = float(value) | |
elif key == 'bg_color': | |
self.apply_bg_color(value) | |
def close_settings(self, settings=None): | |
""" | |
The settings panel has been closed. | |
""" | |
Logger.info("main.py: App.close_settings: {0}".format(settings)) | |
super(MyApp, self).close_settings(settings) | |
class MySettingsWithTabbedPanel(SettingsWithTabbedPanel): | |
""" | |
It is not usually necessary to create subclass of a settings panel. There | |
are many built-in types that you can use out of the box | |
(SettingsWithSidebar, SettingsWithSpinner etc.). | |
You would only want to create a Settings subclass like this if you want to | |
change the behavior or appearance of an existing Settings class. | |
""" | |
def on_close(self): | |
Logger.info("main.py: MySettingsWithTabbedPanel.on_close") | |
def on_config_change(self, config, section, key, value): | |
Logger.info( | |
"main.py: MySettingsWithTabbedPanel.on_config_change: " | |
"{0}, {1}, {2}, {3}".format(config, section, key, value)) | |
if __name__ == '__main__': | |
'''Start the application''' | |
MyApp().run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment