Skip to content

Instantly share code, notes, and snippets.

@villares
Last active April 27, 2023 20:47
Showing SVG with PySimpleGUI
from io import BytesIO
import PySimpleGUI as sg
import cairosvg
# This didn't work with the example file in this gist (the raster background didn't show up)
svg_content = '<svg xmlns="http://www.w3.org/2000/svg" width="100" height="100"><rect x="10" y="10" width="80" height="80"/></svg>'
# filename = 'NdPAbril2.svg'
# with open(filename, 'r') as f:
# svg_content = f.read()
png_bytes = BytesIO()
cairosvg.svg2png(bytestring=svg_content, write_to=png_bytes)
layout = [[sg.Image(data=png_bytes.getvalue())]]
window = sg.Window('SVG Viewer', layout)
while True:
event, values = window.read()
if event == sg.WIN_CLOSED:
break
window.close()
from io import BytesIO
import PySimpleGUI as sg
from svglib.svglib import svg2rlg
from reportlab.graphics import renderPM
filename = 'NdPAbril2.svg' #
drawing = svg2rlg(filename)
io_bytes = BytesIO()
renderPM.drawToFile(drawing, io_bytes, fmt="PNG")
png_bytes = io_bytes.getvalue()
layout = [[sg.Image(png_bytes)]]
window = sg.Window('SVG Viewer', layout)
while True:
event, values = window.read()
if event == sg.WIN_CLOSED:
break
window.close()
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment