0

I have been trying to put a SVG image in tkinter. I have installed cairo and rsvg but when running my script, get the error: AttributeError: module 'rsvg' has no attribute 'Handle' This is my script:

#some code to give rsvg.render_cairo(ctx) ability
#on windows.
import os
try:
    import rsvg
    WINDOWS=False
except ImportError:
    print("Warning, could not import 'rsvg'")
    if os.name == 'nt':
        print("Detected windows, creating rsvg.")
        #some workarounds for windows

        from ctypes import *

        l=CDLL('librsvg-2-2.dll')
        g=CDLL('libgobject-2.0-0.dll')
        g.g_type_init()

        class rsvgHandle():
            class RsvgDimensionData(Structure):
                _fields_ = [("width", c_int),
                            ("height", c_int),
                            ("em",c_double),
                            ("ex",c_double)]

            class PycairoContext(Structure):
                _fields_ = [("PyObject_HEAD", c_byte * object.__basicsize__),
                            ("ctx", c_void_p),
                            ("base", c_void_p)]

            def __init__(self, path):
                self.path = path
                error = ''
                self.handle = l.rsvg_handle_new_from_file(self.path,error)


            def get_dimension_data(self):
                svgDim = self.RsvgDimensionData()
                l.rsvg_handle_get_dimensions(self.handle,byref(svgDim))
                return (svgDim.width,svgDim.height)

            def render_cairo(self, ctx):
                ctx.save()
                z = self.PycairoContext.from_address(id(ctx))
                l.rsvg_handle_render_cairo(self.handle, z.ctx)
                ctx.restore()



        class rsvgClass():
            def Handle(self,file):
                return rsvgHandle(file)

        rsvg = rsvgClass()
h = rsvg.Handle("index.svg")
s = cairo.ImageSurface(cairo.FORMAT_ARGB32, 100, 100)
ctx = cairo.Context(s)
h.render_cairo(ctx)

I am using windows with python 3x.

I don't know what I am doing wrong. Any help would be greatly appreciated.

Andereoo
  • 834
  • 1
  • 8
  • 24
  • Please provide the whole error message. Otherwise we cannot see where the error occurs. – figbeam Jun 02 '19 at 16:40
  • @figbeam The full error is: ```Traceback (most recent call last): File "C:\Users\whoever\filepy", line 5, in import rsvg File "C:\Users\whoever\rsvg.py", line 55, in h = rsvg.Handle("index.svg") AttributeError: module 'rsvg' has no attribute 'Handle'``` – Andereoo Jun 03 '19 at 00:35
  • 2
    It is because your filename is `rsvg.py` and `import rsvg` will import current file and it does not have `Handle` function or class. – acw1668 Jun 03 '19 at 03:33
  • Thanks! I got it to work. – Andereoo Jun 03 '19 at 15:04
  • @Andoo can you please share how you solved the problem? – shantanu rahut Dec 11 '20 at 14:04
  • @shantanurahut I simply renamed the fine from rsvg.py to somethingelse.py. Unfortionately, rsvg still isn't cooperating, though. It gives me a blank screen (see https://stackoverflow.com/questions/55943631/putting-svg-images-into-tkinter-frame) – Andereoo Dec 12 '20 at 15:10
  • @Andoo somebody should solve this rsvg problem on windows. I am gonna get fired over this. -_- – shantanu rahut Dec 13 '20 at 04:59
  • @shantanurahut I feel your pain. I'll check through my old code and see if I can figure out a way to get it to work (I've already spent months on this, though). PyGObject does have an rsvg package, by the way, but to install it on Windows will you need a C compiler. It does work though. It may also be possible to extract PyGObject's rsvg from the rest of the PyGObject package, but I'm not sure. – Andereoo Dec 14 '20 at 13:02
  • @shantanurahut I just came up with a solution (sort of)!!! See https://stackoverflow.com/questions/55943631/putting-svg-images-into-tkinter-frame/65298522#65298522. – Andereoo Dec 15 '20 at 00:47
  • @shantanurahut No problem! Glad I could be helpful. It is funny that in 2020 it is this hard to convert an SVG image. – Andereoo Dec 15 '20 at 14:16

0 Answers0