Changing slider images without using atlas

1,004 views
Skip to first unread message

Carsten Thielepape

unread,
Jan 20, 2013, 11:56:20 AM1/20/13
to kivy-...@googlegroups.com
Hello all,
 
I am not using atlas in my application (by purpose)  (exceotion the config dialog, where I did not find a solution to change this, but this a different story)
 
I like to use the slider widget, but I could not find a solution to change the slider images without using/changing atlas. Is there any solution to assign the images of the slider to an image file at runtime?
 
Thanks!
 
 

Akshay Arora

unread,
Jan 20, 2013, 3:00:51 PM1/20/13
to kivy-...@googlegroups.com
Just override the default kv rule for Slider that's put in style.kv in your own app like so::

from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.lang import Builder

Builder.load_string(
'''
<Slider>:
    canvas:
        Color:
            rgb: 1, 1, 1
        BorderImage:
            border: (0, 18, 0, 18) if self.orientation == 'horizontal' else (18, 0, 18, 0)
            pos: (self.x, self.center_y - 18) if self.orientation == 'horizontal' else (self.center_x - 18, self.y)
            size: (self.width, 37) if self.orientation == 'horizontal' else (37, self.height)
            source: 'slider%s_background.png' % self.orientation[0]
        Rectangle:
            pos: (self.value_pos[0] - 16, self.center_y - 17) if self.orientation == 'horizontal' else (self.center_x - 16, self.value_pos[1] - 16)
            size: (32, 32)
            source: 'slider_cursor.png'

<RootWidget>:
    Slider
''')

class RootWidget(BoxLayout):
    pass

class MainApp(App):

    def build(self):
        return RootWidget()

if __name__ == '__main__':
    MainApp().run()


Don't forget to add the appropriate sliderh_background.png, slider_background.png, slider_cursor.png in your current directory.


 
 

--
 
 

Carsten Thielepape

unread,
Jan 20, 2013, 4:59:58 PM1/20/13
to kivy-...@googlegroups.com
Hello,
 
thanks for your (FAST!) reply, I will give it a try tomorrow. I have to play a little bit with the builder , as I did not use it by now. A further question: Is this a global rule, or can I redefine this widget per widget?
 
E.g,: Can I have different images for two silder on one page?
 
 

Greetings
 
Carsten

Akshay Arora

unread,
Jan 20, 2013, 5:30:56 PM1/20/13
to kivy-...@googlegroups.com
This would be a global rule for your app. You can do the same thing for your own widget by simply sub-classing your own widget from a Slider and writing a rule for your class.

in kv ::

<MySlider>:
    ...
    ...

in .py::

class MySlider(Slider):
    pass

Regards

 
Carsten

--
 
 

Carsten Thielepape

unread,
Mar 15, 2013, 11:58:44 AM3/15/13
to kivy-...@googlegroups.com
Hello,

I did pick up the topic again.

Atlas is not the best solution for me, as I want to have a customer skinnable object.
When I look at the kv language for the slider and the sources, there are 3 elements in the canvas:

  • Color
  • Rectangle
  • BorderImage

So my idea was, to clear the canvas  and to add the elements manually with my images. This works, but I can't move the slider button anymore. To be honest, I could not find any parts in the kivy code, which shows me the "connection" of the touch to the move of the slider button.

Original KV Language Code looks like that:

  
 Color:
 rgb
: 1, 1, 1
 
BorderImage:
 border
: (0, 18, 0, 18) if self.orientation == 'horizontal' else (18, 0, 18, 0)
 pos
: (self.x, self.center_y - 18) if self.orientation == 'horizontal' else (self.center_x - 18, self.y)
 size
: (self.width, 37) if self.orientation == 'horizontal' else (37, self.height)

 source
: 'atlas://data/images/defaulttheme/slider%s_background' % self.orientation[0]

 
Rectangle:
 pos
: (self.value_pos[0] - 16, self.center_y - 17) if self.orientation == 'horizontal' else (self.center_x - 16, self.value_pos[1] - 16)
 size
: (32, 32)

 source
: 'atlas://data/images/defaulttheme/slider_cursor'

My code loks as follows:

 from kivy.uix.slider import Slider
 oSlider
=Slider(min=-100, max=100, value=25,posx=100,posy=100)

 
from kivy.graphics import Color
 
from kivy.graphics import Rectangle
 
from kivy.graphics import BorderImage
 
 oSlider
.canvas.clear()
 oSlider
.canvas.add(Color(1., 1., 1.))
 oSlider
.canvas.add(BorderImage(border=(0, 18, 0, 18), pos= (oSlider.x, oSlider.center_y - 18) ,size= (oSlider.width, 37), source='MYBACKGROUND.png'))
 oSlider
.canvas.add(Rectangle( pos= (oSlider.value_pos[0] - 16, oSlider.center_y - 17), size= (32, 32),source='MYKNOB.png'))
 
BackGround.add_widget(oSlider)


Akshay Arora

unread,
Mar 16, 2013, 3:04:55 PM3/16/13
to kivy-...@googlegroups.com
Replacing kv code with python code won't work unless you understand what the kv code does implicitly(bindings). KV lang handles binding implicitly. I'd suggest doing this in kv as I suggested earlier.

Best Regards

--
You received this message because you are subscribed to the Google Groups "Kivy users support" group.
To unsubscribe from this group and stop receiving emails from it, send an email to kivy-users+...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Reply all
Reply to author
Forward
0 new messages