【发布时间】:2021-04-15 09:31:06
【问题描述】:
我有一个用字符串表示的 SVG 图形
svg_string='<svg height="100" width="500"><ellipse cx="240" cy="50" rx="220" ry="30" style="fill:yellow" /><ellipse cx="220" cy="50" rx="190" ry="20" style="fill:white" /></svg>'
我想在从 Python 启动的窗口上显示由svg_string 表示的图形,即 2 个椭圆。
伪代码应该是这样的
import pygame
def display_svg_figure(screen, svg_string):
# Code that draws the rendered SVG string on
# the screen
pass
background_colour = (255, 255, 255)
(width, height) = (900, 900)
screen = pygame.display.set_mode((width, height))
pygame.display.set_caption('Display SVG')
screen.fill(background_colour)
pygame.display.flip()
svg_string='<svg height="100" width="500"><ellipse cx="240" cy="50" rx="220" ry="30" style="fill:yellow" /><ellipse cx="220" cy="50" rx="190" ry="20" style="fill:white" /></svg>'
display_svg_figure(screen, svg_string)
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
我基本上是在看display_svg_figure(screen,svg_string)方法的一个实现
我也对使用非 pygame 库持开放态度,只要它遵循以下简单接口并且可以将其包含在 pygame 中
from SOME_LIBRARY import SOME_COMPONENT
svg_string="......"
SOME_COMPONENT.display(svg_string)
我已经查看了pynanosvg,但它不再受支持并且失败了,因此不能满足我的需要。
Display SVG file in Python 中给出的解决方案似乎不符合要求,因为它在 PyGame 中不起作用。如果有人可以让它工作,请告诉我。
【问题讨论】:
-
明确一点,你想显示 SVG 字符串的 text,而不是它所代表的图形吗?如果是这样,解决方案不会关心字符串是否为 SVG 格式。
-
@PeterO。为了澄清它应该渲染它所代表的图形。例如,我需要输出是一个显示 svg_string 中提到的 2 个椭圆的窗口。我已经编辑了最初的查询,以便更清楚地提出我的要求。
标签: python python-3.x svg pygame pygame-surface