How to pass parameters to a custom widget?

713 views
Skip to first unread message

Geekademy

unread,
Apr 19, 2017, 10:29:49 PM4/19/17
to kivy-...@googlegroups.com
Hi,

I made an SVG widget:


class SVGWidget(RelativeLayout):
''' SVG needs widget: https://kivy.org/docs/api-kivy.graphics.svg.html '''
def __init__(self, filename, **kwargs):
super().__init__(**kwargs)
svg = Svg(filename) # read svg to find size, is there a better way?
_, _, scale = fit(self.size, (svg.width, svg.height))

with self.canvas:
Scale(scale)
Svg(filename)

It probably could be better, though it works in code. But in the .kv, I can't
figure out how to pass the filename parameter:

#:import SVGWidget widgets.SVGWidget

<SplashPage>:
SVGWidget:
filename: foo.svg

Read a few threads on why this doesn't work, but haven't gotten it to work yet.
Could anyone help?

TypeError: __init__() missing 1 required positional argument: 'filename'


ZenCODE

unread,
Apr 20, 2017, 2:25:11 AM4/20/17
to Kivy users support
Try adding filename as a kivy StringProperty. And perhaps taking out the filename position argument in you Python declaration. Does that fix it?

Geekademy

unread,
Apr 21, 2017, 2:03:44 PM4/21/17
to kivy-...@googlegroups.com
Ok, thanks, I figured it out.

This is how I got it to work (minus the fit part):

class SVGWidgetOwnSize(RelativeLayout):
source = StringProperty()

def __init__(self, **kwargs):
super().__init__(**kwargs)

def on_source(self, instance, value):
self.canvas.clear() # don't add more than once?
with self.canvas:
svg = Svg(value)

self.size = svg.width, svg.height

Unfortunately, I wasn't able to use SVG in this instance and went back to PNG
:-(. Because of:

WARNING Svg: unimplemented command Q

There's some text in there that uses a bezier. Looks like someone started some
code on the subject but didn't get to finish, sigh:

https://github.com/kivy/kivent/blob/master/modules/core/kivent_core/rendering/svg_loader.pyx#L838
Reply all
Reply to author
Forward
0 new messages