Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Draw a SVG image in GTK3 from SVG source in python

Tags:

python

svg

gtk

As I can do it easily in PyQt like this:

img = '''<?xml version="1.0" encoding="UTF-8"?>
<svg width="50" height="50" xmlns="http://www.w3.org/2000/svg">
 <!-- Created with SVG-edit - http://svg-edit.googlecode.com/ -->
 <g>
  <title>Layer 1</title>
   <circle stroke-width="3" stroke="#1a1a1a" fill="#dfdbd2" r="16" cy="25" cx="25"/>
   <path stroke-width="0" fill="#1a1a1a" d="m25,9a16,16 0 0 0 0,32l0,-1.5a18,18 0 0 0 0,-29l0,-1.5z"/>
 </g>
</svg>'''
image = QtCore.QByteArray(img)
self.svgwidget.load(image)

How can I do this in Gtk? any ideas? :) thanks in advance!

like image 549
m.alaa8 Avatar asked Oct 18 '13 14:10

m.alaa8


1 Answers

stream = Gio.MemoryInputStream.new_from_bytes(GLib.Bytes.new(img))
pixbuf = GdkPixbuf.Pixbuf.new_from_stream(stream, None)
self.svgwidget = Gtk.Image.new_from_pixbuf(pixbuf)

Or use the rsvg library if you don't mind an extra dependency and want to do fancy stuff with the SVG once you've loaded it:

svg = Rsvg.Handle.new_from_data(img)
pixbuf = svg.get_pixbuf()
svgwidget = Gtk.Image.new_from_pixbuf(pixbuf)
like image 118
ptomato Avatar answered Sep 19 '22 20:09

ptomato