Skip to content

Instantly share code, notes, and snippets.

@clayote
Created November 28, 2013 00:43
Show Gist options
  • Save clayote/7685603 to your computer and use it in GitHub Desktop.
Save clayote/7685603 to your computer and use it in GitHub Desktop.
"""A graphical selector for "swatches," which may be colors, textures,
or standalone sprites."""
from kivy.uix.gridlayout import GridLayout
from kivy.uix.togglebutton import ToggleButton
from kivy.properties import (
NumericProperty,
StringProperty,
ObjectProperty)
kv = """
<Swatch>:
BoxLayout:
orientation: 'vertical'
pos: root.pos
size: root.size
Image:
texture: root.display_texture
Label:
text: root.display_text
color: root.color
disabled_color: root.disabled_color
font_name: root.font_name
font_size: root.font_size
disabled: root.state == 'normal'
"""
class Swatch(ToggleButton):
display_text = StringProperty()
display_texture = ObjectProperty()
class SwatchBox(GridLayout):
texdict = ObjectProperty()
style = ObjectProperty()
finality = NumericProperty(0)
def on_texdict(self, i, v):
self.finality += 1
def on_style(self, i, v):
self.finality += 1
def on_parent(self, i, v):
self.finality += 1
def on_finality(self, i, v):
if v == 3:
self.finalize()
def finalize(self):
for (key, val) in self.texdict.iteritems():
self.add_widget(Swatch(
display_text=key, display_texture=val,
background_color=self.style.bg_active.rgba,
color=self.style.text_active.rgba,
disabled_color=self.style.text_inactive.rgba,
font_name=self.style.fontface,
font_size=self.style.fontsize))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment