在python中从SVG源码在GTK3中绘制SVG图像 [英] Draw a SVG image in GTK3 from SVG source in python

查看:409
本文介绍了在python中从SVG源码在GTK3中绘制SVG图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



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)

我如何在Gtk中执行此操作?有任何想法吗? :)提前感谢!

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

推荐答案

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)

或者使用rsvg库,如果你不介意额外的依赖,并且想在SVG加载时使用SVG: p>

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)

这篇关于在python中从SVG源码在GTK3中绘制SVG图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆