Can't pass string from .kv file to class method

60 views
Skip to first unread message

Forbidden 303

unread,
Aug 22, 2016, 9:32:16 PM8/22/16
to Kivy users support

I'm trying to load an svg ( 'add.svg' ) file onto the Widget canvas by passing the filename via the kv file's widget property.

If I pass the filename as a string to the my_svg variable it loads fine, but i want to be able to dynamically load them from the kv file.

class SvgWidget(Scatter):
   
    my_svg = StringProperty('', allownone=True)

    def __init__(self, **kwargs):
        super(SvgWidget, self).__init__(**kwargs)
        filename = self.my_svg
        with self.canvas:
            svg = Svg(filename)
            self.size = svg.width, svg.height


and in my kv file:

<MyBoxLayout>:
   
    SvgWidget:
        my_svg: 'add.svg'
        id: addbtn
        scale: 10.

Any help would be much appreciated. :)

ZenCODE

unread,
Aug 23, 2016, 2:47:37 AM8/23/16
to Kivy users support
Then you can put it in the 'on_my_svg' events? That should do ite.g.

class SvgWidget(Scatter):
    
    my_svg = StringProperty('', allownone=True)

    def on_my_svg(self, widget, value):
        self.canvas.clear()  # Or is it Clear? 
        with self.canvas:
            svg = Svg(value)
            self.size = svg.width, svg.height

Forbidden 303

unread,
Aug 23, 2016, 5:05:09 PM8/23/16
to Kivy users support
That did the trick.

Having fun with kivy, but run into little trips like this, scratching my head for hours...lol

Much obliged

:)

Forbidden 303

unread,
Aug 23, 2016, 5:48:28 PM8/23/16
to Kivy users support
One more thing. :o

if i want to scale the svg to the entire canvas size  of a given widget, is this possible?

Thanks.

ZenCODE

unread,
Aug 24, 2016, 1:55:31 AM8/24/16
to Kivy users support
Of course. You can calculate the how much you need to scale it by, then set the scale property of the scatter.

scale = float(widget.width) / float(svg.width)  # Do the same for height & use the smaller one.

Reply all
Reply to author
Forward
0 new messages