Svg Image Not Resizing & Repositioning

61 views
Skip to first unread message

Anoop Rana

unread,
May 4, 2020, 9:13:33 AM5/4/20
to kivy-...@googlegroups.com
Hi i have displayed an svg image on screen but as you can see in the video the image is not repositioning and resizing itself according to the screen size.It should remain at the center of the screen when the screen size changes .How can i achieve that effect?Below is the code i am using.I am attaching a video.

<MainWIndow>:
       FloatLayout:
           id:crop

class SvgWidget(Scatter):

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



class MainWIndow(Screen):
    def on_enter(self):
            svg = SvgWidget('IMAGE.svg', size_hint=(None, None))
            self.ids.crop.add_widget(svg)
            svg.scale = .5
            svg.center = Window.center
Screencast 2020-05-04 18:35:56.mp4

Robert Flatt

unread,
May 4, 2020, 12:50:01 PM5/4/20
to Kivy users support


Warning

This is highly experimental and subject to change. Don’t use it in production.


Looks like you will have to dig into the svg code.

Elliot Garbus

unread,
May 5, 2020, 10:52:05 AM5/5/20
to Kivy users support
I did a few experiments and get get an SVG to scale with the window size, I can't seem to position the widget.  The documentation describes anchor_x and anchor_y... but they are not behaving as i would expect.  I hope this helps... I might play with this a little more as time allows.

from kivy.app import App
from kivy.lang import Builder
from kivy.graphics.svg import Svg
from kivy.uix.widget import Widget
from kivy.properties import StringProperty
from kivy.graphics import Scale

kv = """
#:import Svg kivy.graphics.svg.Svg
GridLayout:
cols: 2
rows: 2
id: grid
BoxLayout:
id:box
Label:
text: 'Test'
Label:
text: 'Test'
Label:
text: 'Test'

"""

class SvgWidget(Widget):
filename = StringProperty()

def __init__(self, **kwargs):
f = kwargs.pop('filename')
super().__init__(**kwargs)
with self.canvas:
self.scale = Scale(1, 1, 1)
self.svg = Svg(f)
self.bind(size=self._do_resize)
self.bind(pos=self._do_position)

def _do_position(self, *args):
# self.svg.anchor_x = self.x
#self.svg.anchor_y = self.y

print(dir(self.svg))
#print(self.svg.anchor_y)

def _do_resize(self, *args):
self.scale.xyz = (self.width / self.svg.width, self.height / self.svg.height, 1)


class TestSvgApp(App):
def build(self):
return Builder.load_string(kv)

def on_start(self):
s = SvgWidget(filename='acid.svg')
self.root.ids.box.add_widget(s)

TestSvgApp().run()
Reply all
Reply to author
Forward
0 new messages