/usr/share/pyshared/kivy/uix/slider.py is in python-kivy 1.7.2-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 | '''
Slider
======
.. image:: images/slider.jpg
The :class:`Slider` widget looks like a scrollbar. It supports horizontal and
vertical orientation, min/max and a default value.
To create a slider from -100 to 100 starting at 25::
from kivy.uix.slider import Slider
s = Slider(min=-100, max=100, value=25)
To create a vertical slider::
from kivy.uix.slider import Slider
s = Slider(orientation='vertical')
'''
__all__ = ('Slider', )
from kivy.uix.widget import Widget
from kivy.properties import NumericProperty, AliasProperty, OptionProperty, \
ReferenceListProperty, BoundedNumericProperty
class Slider(Widget):
'''Class for creating Slider widget.
Check module documentation for more details.
'''
value = NumericProperty(0.)
'''Current value used for the slider.
:data:`value` is a :class:`~kivy.properties.NumericProperty`, default to 0.
'''
min = NumericProperty(0.)
'''Minimum value allowed for :data:`value`.
:data:`min` is a :class:`~kivy.properties.NumericProperty`, default to 0.
'''
max = NumericProperty(100.)
'''Maximum value allowed for :data:`value`.
:data:`max` is a :class:`~kivy.properties.NumericProperty`, default to 100.
'''
padding = NumericProperty(10)
'''Padding of the slider. The padding is used for graphical representation
and interaction. It prevents the cursor from going out of the bounds of the
slider bounding box.
By default, padding is 10. The range of the slider is reduced from padding *
2 on the screen. It allows drawing a cursor of 20px width, without having
the cursor going out of the widget.
:data:`padding` is a :class:`~kivy.properties.NumericProperty`, default to
10.
'''
orientation = OptionProperty('horizontal', options=(
'vertical', 'horizontal'))
'''Orientation of the slider.
:data:`orientation` is an :class:`~kivy.properties.OptionProperty`, default
to 'horizontal'. Can take a value of 'vertical' or 'horizontal'.
'''
range = ReferenceListProperty(min, max)
'''Range of the slider, in the format (minimum value, maximum value)::
>>> slider = Slider(min=10, max=80)
>>> slider.range
[10, 80]
>>> slider.range = (20, 100)
>>> slider.min
20
>>> slider.max
100
:data:`range` is a :class:`~kivy.properties.ReferenceListProperty` of
(:data:`min`, :data:`max`)
'''
step = BoundedNumericProperty(0, min=0)
'''Step size of the slider.
.. versionadded:: 1.4.0
Determines the size of each interval or step the slider takes between
min and max. If the value range can't be evenly divisible by step the
last step will be capped by slider.max
:data:`step` is a :class:`~kivy.properties.NumericProperty`, default to 1.
'''
def get_norm_value(self):
vmin = self.min
d = self.max - vmin
if d == 0:
return 0
return (self.value - vmin) / float(d)
def set_norm_value(self, value):
vmin = self.min
step = self.step
val = value * (self.max - vmin) + vmin
if step == 0:
self.value = val
else:
self.value = min(round((val - vmin) / step) * step + vmin, self.max)
value_normalized = AliasProperty(get_norm_value, set_norm_value,
bind=('value', 'min', 'max', 'step'))
'''Normalized value inside the :data:`range` (min/max) to 0-1 range::
>>> slider = Slider(value=50, min=0, max=100)
>>> slider.value
50
>>> slider.value_normalized
0.5
>>> slider.value = 0
>>> slider.value_normalized
0
>>> slider.value = 100
>>> slider.value_normalized
1
You can also use it for setting the real value without knowing the minimum
and maximum::
>>> slider = Slider(min=0, max=200)
>>> slider.value_normalized = .5
>>> slider.value
100
>>> slider.value_normalized = 1.
>>> slider.value
200
:data:`value_normalized` is an :class:`~kivy.properties.AliasProperty`.
'''
def get_value_pos(self):
padding = self.padding
x = self.x
y = self.y
nval = self.value_normalized
if self.orientation == 'horizontal':
return (x + padding + nval * (self.width - 2 * padding), y)
else:
return (x, y + padding + nval * (self.height - 2 * padding))
def set_value_pos(self, pos):
padding = self.padding
x = min(self.right - padding, max(pos[0], self.x + padding))
y = min(self.top - padding, max(pos[1], self.y + padding))
if self.orientation == 'horizontal':
if self.width == 0:
self.value_normalized = 0
else:
self.value_normalized = (x - self.x - padding
) / float(self.width - 2 * padding)
else:
if self.height == 0:
self.value_normalized = 0
else:
self.value_normalized = (y - self.y - padding
) / float(self.height - 2 * padding)
value_pos = AliasProperty(get_value_pos, set_value_pos,
bind=('x', 'y', 'width', 'height', 'min',
'max', 'value_normalized', 'orientation'))
'''Position of the internal cursor, based on the normalized value.
:data:`value_pos` is an :class:`~kivy.properties.AliasProperty`.
'''
def on_touch_down(self, touch):
if self.collide_point(*touch.pos):
if touch.is_mouse_scrolling:
if 'down' in touch.button or 'left' in touch.button:
if self.step:
self.value = min(self.max, self.value + self.step)
else:
self.value = min(
self.max,
self.value + (self.max - self.min) / 20)
if 'up' in touch.button or 'right' in touch.button:
if self.step:
self.value = max(self.min, self.value - self.step)
else:
self.value = max(
self.min,
self.value - (self.max - self.min) / 20)
else:
touch.grab(self)
self.value_pos = touch.pos
return True
def on_touch_move(self, touch):
if touch.grab_current == self:
self.value_pos = touch.pos
return True
def on_touch_up(self, touch):
if touch.grab_current == self:
self.value_pos = touch.pos
return True
if __name__ == '__main__':
from kivy.app import App
class SliderApp(App):
def build(self):
return Slider(padding=25)
SliderApp().run()
|