[英]Draw a SVG image in GTK3 from SVG source in python
As I can do it easily in PyQt like this: 我可以在PyQt中轻松完成这样的操作:
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? 我怎么能在Gtk中这样做? any ideas?
有任何想法吗? :) thanks in advance!
:) 提前致谢!
For anyone who wants to load and scale a SVG image using Gtk 3 and Python (while preserving the SVG image quality!): 对于任何想要使用Gtk 3和Python加载和缩放SVG图像的人(同时保留SVG图像质量!):
from gi.repository import Gtk, GdkPixbuf
path = "/path/to/image.svg"
width = -1
height = 128
preserve_aspect_ratio = True
pixbuf = GdkPixbuf.Pixbuf.new_from_file_at_scale(path, width, height, preserve_aspect_ratio)
image = Gtk.Image()
image.set_from_pixbuf(pixbuf)
See also https://developer.gnome.org/gdk-pixbuf/stable/gdk-pixbuf-File-Loading.html#gdk-pixbuf-new-from-file-at-scale 另见https://developer.gnome.org/gdk-pixbuf/stable/gdk-pixbuf-File-Loading.html#gdk-pixbuf-new-from-file-at-scale
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库:
svg = Rsvg.Handle.new_from_data(img)
pixbuf = svg.get_pixbuf()
svgwidget = Gtk.Image.new_from_pixbuf(pixbuf)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.