Created
January 13, 2014 05:21
-
-
Save knappador/8395143 to your computer and use it in GitHub Desktop.
Just an example of some kv used in practice
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
# meanwhile in some widget tree I create an instance and | |
# set the property in that specific tree for the specific ColorSel instance | |
ColorSel: | |
title: 'high color' | |
# passing in the attribute that will be bound inside kv | |
bind_color: 'ebbh' | |
<ColorSel@RelativeLayout>: | |
swatch: swatch | |
hue_slider: hue_slider | |
val_slider: val_slider | |
Label: | |
halign: 'left' | |
valign: 'middle' | |
text_size: self.size | |
font_size: self.height * 0.7 | |
size_hint: d_ctl.l1_sh | |
pos_hint: d_ctl.l1_ph | |
text: 'color' | |
Slider: | |
id: hue_slider | |
range: (0., 1.) | |
size_hint: d_ctl.cs1_sh | |
pos_hint: d_ctl.cs1_ph | |
on_value: root.hue = self.value | |
Label: | |
halign: 'left' | |
valign: 'middle' | |
size_hint: d_ctl.l2_sh | |
pos_hint: d_ctl.l2_ph | |
text: 'light' | |
text_size: self.size | |
font_size: self.height * 0.7 | |
Slider: | |
id: val_slider | |
range: (0., 1.) | |
size_hint: d_ctl.cs2_sh | |
pos_hint: d_ctl.cs2_ph | |
on_value: root.val = self.value | |
RelativeLayout: | |
size_hint: d_ctl.swatch_sh | |
pos_hint: d_ctl.swatch_ph | |
Swatch: | |
id: swatch | |
Label: | |
halign: 'center' | |
valign: 'middle' | |
size_hint: (0.9, 0.9) | |
pos_hint: {'x' : 0.05, 'y' : 0.05} | |
text: root.title | |
text_size: self.size | |
font_size: self.height * 0.5 | |
class ColorSel(RelativeLayout): | |
hue = NumericProperty() | |
val = NumericProperty() | |
title = StringProperty() | |
bind_color = StringProperty() | |
def on_hue(self, i, hue): | |
# here's where I'm using the value set in the kv file | |
getattr(c_ctl, self.bind_color)[0] = hue | |
def on_val(self, i, val): | |
# here's where I'm using the value set in the kv file | |
getattr(c_ctl, self.bind_color)[2] = val | |
def on_bind_color(self, i, color): | |
# here's where I'm using the value set in the kv file | |
self.hue = getattr(c_ctl, color)[0] | |
self.val = getattr(c_ctl, color)[2] | |
self.hue_slider.value = self.hue | |
self.val_slider.value = self.val | |
self.swatch.color = getattr(c_ctl, color) | |
# this use of kwargs unpacking is pretty damned slick | |
# if I must say so. color is a string, but I need | |
# the equivalent of passing in a named argument...so a dictionary, unpacked | |
c_ctl.bind(**{color : self.swatch.setter('color')}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment