如何在 Python 中显示 SVG 图像 [英] How to display an SVG image in Python
问题描述
我正在关注本教程 关于如何用 Python 编写国际象棋程序.
I was following this tutorial on how to write a chess program in Python.
它使用 python-chess 引擎.该引擎的函数显然返回 SVG 数据,可用于显示棋盘.
It uses the python-chess engine. The functions from that engine apparently return SVG data, that could be used to display a chessboard.
- 教程中的代码:
import chess
import chess.svg
from IPython.display import SVG
board = chess.Board()
SVG(chess.svg.board(board=board,size=400))
但是当我运行该代码时,我看到的只是终端中的一行,而没有图像.
but when I run that code, all I see is a line in the terminal and no image.
本教程对 Jupyter Notebooks 进行了传递参考以及如何它们可用于显示 SVG 图像.我没有使用 Jupyter Notebooks 的经验,即使我从 pip 安装了该软件包并且我对如何使用它有所涉猎,但在解决我原来的棋盘问题方面并没有取得太大进展.但我确实拥有使用 C++ 进行 Qt 开发的经验,并且由于 Qt 具有 Python 绑定,我决定使用这些绑定.
The tutorial makes a passing reference to Jupyter Notebooks and how they can be used to display SVG images. I have no experience with Jupyter Notebooks and even though I installed the package from pip and I dabbled a little into how to use it, I couldn't make much progress with regards to my original chessboard problem. But what I do have, is, experience with Qt development using C++ and since Qt has Python bindings, I decided to use those bindings.
这是我写的:
import sys
import chess
import chess.svg
from PyQt5 import QtGui, QtSvg
from PyQt5.QtWidgets import QApplication
from IPython.display import SVG, display
app = QApplication(sys.argv);
board = chess.Board();
svgWidget = QtSvg.QSvgWidget(chess.svg.board(board=board, size=400));
#svgWidget.setGeometry(50,50,759,668)
svgWidget.show()
sys.exit(app.exec_())
一个 Qt 窗口打开并没有显示任何内容,在终端中我看到了很多文本 - (显然 SVG 数据最终出现在控制台中,而不是在打开的 Qt 窗口中?).
A Qt window opens and shows nothing and in the terminal I see a lot of text - (apparently the SVG data is ending up in the console and not in the Qt window that is opening?).
我想我必须在 python 下安装一些 SVG 库,所以我从 pip 安装了 drawSvg.但似乎库生成了 SVG 图像.而且对我没用.
I figured I have to install some SVG library under python so I installed drawSvg from pip. But it seems that library generates SVG images. And was of no use for me.
更奇怪的是,看到之后这个问题,我尝试了以下方法:
What is even more strange is, after seeing this SO question, I tried the following:
import sys
import chess
import chess.svg
from PyQt5 import QtGui, QtSvg
from PyQt5.QtWidgets import QApplication
from IPython.display import SVG, display
app = QApplication(sys.argv);
board = chess.Board();
svgWidget = QtSvg.QSvgWidget('d:\projects\python_chess\Zeichen_123.svg');
#svgWidget.setGeometry(50,50,759,668)
svgWidget.show()
sys.exit(app.exec_())
它显示了一个图像 - 一个 SVG 图像!那么我的案子和这个案子有什么区别?
And it showed an image - an SVG image! What is the difference then between my case and this case?
问题: 所以我的问题是,在棋盘 SVG 数据的情况下,我做错了什么?python-chess库生成的SVG数据是不是和QtSvg不兼容?
Question: So my question is, what I am doing wrong in the case of the chessboard SVG data? Is the SVG data generated by the python-chess library not compatible with QtSvg?
推荐答案
我认为您对 Python 的脚本性质感到困惑.你说,你有C++下Qt开发的经验.难道您不首先在那里创建一个主窗口小部件,然后向其中添加您将在其中调用或加载 SVG 数据的 SVG 小部件吗?
I think you are getting confused by the scripting nature of Python. You say, you have experience with Qt development under C++. Wouldn't you create a main window widget there first and add to it your SVG widget within which you would call or load SVG data?
我会像这样重写你的代码.
I would rewrite your code something like this.
import chess
import chess.svg
from PyQt5.QtSvg import QSvgWidget
from PyQt5.QtWidgets import QApplication, QWidget
class MainWindow(QWidget):
def __init__(self):
super().__init__()
self.setGeometry(100, 100, 1100, 1100)
self.widgetSvg = QSvgWidget(parent=self)
self.widgetSvg.setGeometry(10, 10, 1080, 1080)
self.chessboard = chess.Board()
self.chessboardSvg = chess.svg.board(self.chessboard).encode("UTF-8")
self.widgetSvg.load(self.chessboardSvg)
if __name__ == "__main__":
app = QApplication([])
window = MainWindow()
window.show()
app.exec()
编辑
如果在 MainWindow 类中添加一个绘制函数会更好.因为可以肯定的是,将来,每当您移动一块时,您都会想要多次重新绘制您的棋盘图像.所以我会做这样的事情.
It would be even better if you would add a paint function to the MainWindow class. Because for sure in future, you would want to repaint your board image many times, whenever you would move a piece. So I would do something like this.
def paintEvent(self, event):
self.chessboardSvg = chess.svg.board(self.chessboard).encode("UTF-8")
self.widgetSvg.load(self.chessboardSvg)
这篇关于如何在 Python 中显示 SVG 图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!