title: Putting .SVG images into tkinter Frame
body: <p>I have been trying to put the image from <a href="https://betacssjs.chesscomfiles.com/bundles/web/favicons/safari-pinned-tab.f387b3f2.svg" rel="noreferrer">https://betacssjs.chesscomfiles.com/bundles/web/favicons/safari-pinned-tab.f387b3f2.svg</a> into a Tkinter frame. I found from the post <a href="https://stackoverflow.com/questions/22583035/can-you-display-an-image-inside-of-a-python-program-without-using-pygame">here</a> that it is possible with the help from rsvg and cairo.</p>
  
  <p>I was using python 3.6 on Windows 10. I got rsvg from <a href="https://github.com/GNOME/librsvg" rel="noreferrer">here</a> and cairo from <a href="https://github.com/vishnubob/cairo" rel="noreferrer">here</a> and then extracted the folders to the 'C:\Users...\site_packages' folder. They import fine but I cannot figure out how to use them. I tried using the code:</p>
  
  <pre><code>import tkinter as tk
  main=tk.Tk()
  frame=tk.Frame(main)
  def svgPhotoImage(self,file_path_name):
          from PIL import Image,ImageTk
          import rsvg,cairo 
          svg = rsvg.Handle(file=file_path_name)
          width, height = svg.get_dimension_data()[:2]
              surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, int(width), int(height))
              context = cairo.Context(surface)
              #context.set_antialias(cairo.ANTIALIAS_SUBPIXEL)
              svg.render_cairo(context)
              tk_image=ImageTk.PhotoImage('RGBA')
              image=Image.frombuffer('RGBA',(width,height),surface.get_data(),'raw','BGRA',0,1)
              tk_image.paste(image)
              return(tk_image)
      tk_image=self.svgPhotoImage(filename)
      frame.configure(image=tk_image)
  </code></pre>
  
  <p>and </p>
  
  <pre><code>#rsvg.py
  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("box.svg")
  s = cairo.ImageSurface(cairo.FORMAT_ARGB32, 100, 100)
  ctx = cairo.Context(s)
  h.render_cairo(ctx)
  </code></pre>
  
  <p>After trying those scripts, I kept getting the error message:</p>
  
  <pre><code>AttributeError: module 'rsvg' has no attribute 'Handle'
  </code></pre>
  
  <p>I am sure I did something wrong in the process but after hours of searching still could not figure out how to get it to work. I also tried installing pycairo (via pip) but got the error message </p>
  
  <pre><code>ERROR: Command "'c:\...\python36-32\python.exe' -u -c 'import setuptools, tokenize;__file__='"'"'C:\\...\\pip-install-peqhj3x1\\pycairo\\setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' install --record 'C:\...\Temp\pip-record-jfozfbuc\install-record.txt' --single-version-externally-managed --compile" failed with error code 1 in C:\...\pip-install-peqhj3x1\pycairo\
  </code></pre>
  
  <p>I have no clue as to what to do now</p>
  
  <blockquote>
    <p>EDIT: I was finally able to obtain pycairo from
    <a href="https://www.lfd.uci.edu/~gohlke/pythonlibs/" rel="noreferrer">https://www.lfd.uci.edu/~gohlke/pythonlibs/</a> and now  have it working.
    I finally got rsvg to not give me the error message above by referring
    <a href="https://stackoverflow.com/questions/56414829/attributeerror-module-rsvg-has-no-attribute-handle">here</a>
    and got the nessary DLL files from
    <a href="https://github.com/jmcb/python-rsvg-dependencies" rel="noreferrer">here</a>. <strong>Cairo is
    working fine but RSVG is creating a blank screen.</strong> I tried another
    7-liner
    <a href="https://stackoverflow.com/questions/56463524/python-svg-converter-creates-empty-file">here</a>
    and get the output of a blank file instead of a converted one.
    Evidently RSVG is not working and I think it is an installation issue
    (eg. incorrect .dll's). Help?</p>
  </blockquote>
  
  <p>If am trying to use cairo and rsvg because they take up little space and are really fast; wand or some other library is not an option. I just want to be able to put an SVG file into a tkinter frame. If anyone knows how install rsvg properly, I'd appreciate knowing. </p>
  
  <p>Any help would be hugely appreciated.
  Thank you for any suggestions.</p>