10

I was wondering whether it was possible to displays kivy spinner values as a moving label, so that the user knows exactly what the current value of the slider is.

Thanks

The armateur
  • 103
  • 1
  • 1
  • 4
  • You can write `on_touch_move: myFunctionOnMove()` as a property of your `Slider` class in your kv file and then add the `self.myFunctionOnMove()` to your python source to do what you want it to do. –  Sep 04 '16 at 22:17

1 Answers1

14

you just bind a listener to the value change event

some_label = Label(...)
my_slider = Slider(...)
def OnSliderValueChange(instance,value):
    some_label.text = str(value)

my_slider.bind(value=OnSliderValueChange)

as inclement points out in the .kv file you could do something like

<PongGame>:
    ...
    canvas:
        Rectangle:
            ...
    Label:
        ...
        text: str(slider_id.value)
     Slider:
        ...
        id: slider_id
Joran Beasley
  • 110,522
  • 12
  • 160
  • 179
  • Thanks Joran, that was one armateurs question. How would you go about doing it from a kv file? For TextInput I can use 'on_focus:root.setLabel(self.text)' but I cant find a way to do the same thing for Spinner or Slider – The armateur Apr 02 '14 at 19:21
  • use the `on_value` I think (i think `.bind(some_var=handler_fn)` is roughly equal to `on_some_var` in the kv files – Joran Beasley Apr 02 '14 at 19:23
  • on_value does the job for Slider. And 'on_text' does it for Spinner ==> problem solved! Thank you – The armateur Apr 02 '14 at 19:29
  • 2
    In kv you don't need to use the `on_value` event if the widgets are in the same rule. You can just give the slider an id (`id: slider_id`) and then in the Label write `text: str(slider_id.value)`. The kv language will automatically detect the reference and create the binding for you. – inclement Apr 02 '14 at 19:31
  • wow even better answer thanks inclement :) – Joran Beasley Apr 02 '14 at 19:33
  • 1
    This answer helped me in this particular case, but where in the docs can I find this? Looking at the [API ref](https://kivy.org/docs/api-kivy.uix.slider.html) I do not see which events can be handled, and which parameters would be fed to the event handler. – gens Oct 13 '16 at 14:13