Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/svg/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x 如何从string/xml流在PyQT5中显示SVG图像?_Python 3.x_Svg_Qt5_Pyqt5 - Fatal编程技术网

Python 3.x 如何从string/xml流在PyQT5中显示SVG图像?

Python 3.x 如何从string/xml流在PyQT5中显示SVG图像?,python-3.x,svg,qt5,pyqt5,Python 3.x,Svg,Qt5,Pyqt5,在Python3/QT5应用程序中,我试图显示最初构建为字符串的SVG图像。我需要操纵这个SVG图像(例如,改变它的颜色),这样字符串就会随着时间的推移而改变。下面是一个简单的工作示例: import sys from PyQt5.QtWidgets import QApplication from PyQt5.QtSvg import QSvgWidget svg_str = """<?xml version="1.0" encoding="UTF-8" standalone="no"

在Python3/QT5应用程序中,我试图显示最初构建为字符串的SVG图像。我需要操纵这个SVG图像(例如,改变它的颜色),这样字符串就会随着时间的推移而改变。下面是一个简单的工作示例:

import sys
from PyQt5.QtWidgets import QApplication
from PyQt5.QtSvg import QSvgWidget

svg_str = """<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<svg width="300" height="300" viewBox="0 0 300 300" id="smile" version="1.1">
    <path
        style="fill:#ffaaaa"
        d="M 150,0 A 150,150 0 0 0 0,150 150,150 0 0 0 150,300 150,150 0 0 0 
            300,150 150,150 0 0 0 150,0 Z M 72,65 A 21,29.5 0 0 1 93,94.33 
            21,29.5 0 0 1 72,124 21,29.5 0 0 1 51,94.33 21,29.5 0 0 1 72,65 Z 
            m 156,0 a 21,29.5 0 0 1 21,29.5 21,29.5 0 0 1 -21,29.5 21,29.5 0 0 1 
            -21,-29.5 21,29.5 0 0 1 21,-29.5 z m -158.75,89.5 161.5,0 c 0,44.67 
            -36.125,80.75 -80.75,80.75 -44.67,0 -80.75,-36.125 -80.75,-80.75 z"
    />
</svg>
"""
# ==========================================
with open('smile.svg', 'w') as f:
    f.write(svg_str)
# ==========================================
app = QApplication(sys.argv)
svgWidget = QSvgWidget('smile.svg')
svgWidget.setGeometry(100,100,300,300)
svgWidget.show()
sys.exit(app.exec_())

但我并不满意,因为我需要扩展
QSvgWidget
类,以便从
xml
字符串实例化它。我的问题是:是否有任何方法不必求助于
QPaint
paintEvent

您可以获得
QSvgWidget
使用的
qsvgRender
来渲染SVG。它允许从文件、字节数组或流加载数据:

import sys
from PyQt5.QtWidgets import QApplication
from PyQt5.QtSvg import QSvgWidget, QSvgRenderer

svg_str = """<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<svg width="300" height="300" viewBox="0 0 300 300" id="smile" version="1.1">
    <path
        style="fill:#ffaaaa"
        d="M 150,0 A 150,150 0 0 0 0,150 150,150 0 0 0 150,300 150,150 0 0 0 
            300,150 150,150 0 0 0 150,0 Z M 72,65 A 21,29.5 0 0 1 93,94.33 
            21,29.5 0 0 1 72,124 21,29.5 0 0 1 51,94.33 21,29.5 0 0 1 72,65 Z 
            m 156,0 a 21,29.5 0 0 1 21,29.5 21,29.5 0 0 1 -21,29.5 21,29.5 0 0 1 
            -21,-29.5 21,29.5 0 0 1 21,-29.5 z m -158.75,89.5 161.5,0 c 0,44.67 
            -36.125,80.75 -80.75,80.75 -44.67,0 -80.75,-36.125 -80.75,-80.75 z"
    />
</svg>
"""

svg_bytes = bytearray(svg_str, encoding='utf-8')

app = QApplication(sys.argv)
svgWidget = QSvgWidget()
svgWidget.renderer().load(svg_bytes)
svgWidget.setGeometry(100,100,300,300)
svgWidget.show()
sys.exit(app.exec_())
导入系统 从PyQt5.QtWidgets导入QApplication 从PyQt5.QtSvg导入QSvgWidget、qsvgrender svg_str=”“” """ svg_bytes=bytearray(svg_str,编码='utf-8') app=QApplication(sys.argv) svgWidget=QSvgWidget() svgWidget.renderer().load(svg_字节) svgWidget.setGeometry(100100300300) svgWidget.show() sys.exit(app.exec_())
此处的更多信息:

如果您不需要
QSvgWidget
例如,您希望获得
QImage
(您可以从中获得
QPixmap
QIcon
),您可以使用:

qimage = QtGui.QImage.fromData(svg_bytes)
确保
svg
位于

qimage = QtGui.QImage.fromData(svg_bytes)