Skip to content
Advertisement

Kivy: best way to make a single widget containing different widgets and being able to access their properties in the kv file

I’m trying to make a class that would be a Slider + 2 Labels, one label showing the value of the slider, and one showing the name of the slider. The goal is to reuse this in lieu of Slider when convinient.

I am ending up creating properties in the my class MySlider reflecting the ones I am interested in in Slider. If, for example, I wanted to be able to also specify the color of the value label, I could do the same, add a property “value_label_color” to MySlider and set the label’s color in on_value_label_color()… Then in the kv file I would do something like that:

JavaScript

This would work but could be very long if I had many widgets in my MySlider class.

I thought there may be a way to avoid all this and directly access the label’s color from the kv file, so I could do something like the following?

JavaScript

But this doesn’t work.

Here is my minimal code as of now: .py:

JavaScript

and the kv file:

JavaScript

If you have any suggestion about how to get there in the nicest (and economical) way, I would be grateful.

Thanks a lot. C

Advertisement

Answer

You can do that all from the kv file. In fact, you don’t even need to define the MySlider class in the python code. You can modify your kv like this:

JavaScript

and your python code simplifies to:

JavaScript
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement