尝试使用QSvgWidget显示动画SVG。适用于svg的animateTranform,但不适用于简单的动画。代码如下:
import sys
from PyQt5.QtWidgets import QApplication
from PyQt5.QtSvg import QSvgWidget, QSvgRenderer
# works (but rectangle takes whole window; renderer().animated() = True)
'''
<svg>
<rect x="10" y="10" height="50" width="50"
style="stroke:#ff0000; fill: #0000ff">
<animateTransform
attributeName="transform"
begin="0s"
dur="20s"
type="rotate"
from="0 60 60"
to="360 60 60"
repeatCount="indefinite"
/>
</rect>
</svg>
'''
# not working (image does not move, does not have right size; renderer().animated() = False)
svg_str = '''
<svg>
<rect x="10" y="10" height="50" width="50"
style="stroke:#ff0000; fill: #0000ff">
<animate
attributeName="x"
begin="0s"
dur="2s"
values="0;50;20;100"
keyTimes="0;0.5;0.9;1"
fill="freeze"
/>
</rect>
</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)
print(svgWidget.renderer().animated())
svgWidget.show()
sys.exit(app.exec_())
还尝试将svg代码放置在单独的svg文件中,以及将svg小部件放置在具有布局的QMainWindow中,但不会进行任何更改。
我想念什么吗? svg小部件仅支持animateTransform吗?