返回SVG对象或XML字符串而不是Image

时间:2017-04-29 11:50:08

标签: python python-3.x svg

嘿,伙计们,我是初级程序员,在这个特定的应用程序中遇到了麻烦。

我想从python-chess库中渲染一个棋盘,它总是返回一个SVG xml字符串而不是图像。这是文档python-chess

>>> chess.svg.piece(chess.Piece.from_symbol("R"))
'<svg version="1.1" viewBox="0 0 45 45" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"><g class="white rook" fill="#fff" fill-rule="evenodd" id="white-rook" stroke="#000" stroke-linecap="round" stroke-linejoin="round" stroke-width="1.5"><path d="M9 39h27v-3H9v3zM12 36v-4h21v4H12zM11 14V9h4v2h5V9h5v2h5V9h4v5" stroke-linecap="butt" /><path d="M34 14l-3 3H14l-3-3" /><path d="M31 17v12.5H14V17" stroke-linecap="butt" stroke-linejoin="miter" /><path d="M31 29.5l1.5 2.5h-20l1.5-2.5" /><path d="M11 14h23" fill="none" stroke-linejoin="miter" /></g></svg>'

我试图用svgwrite渲染这个但是无法做到。

在python中有没有其他方法我可以直接将所述XML呈现给图像并在屏幕上显示?

还尝试使用IPython.display模块尝试显示SVG,但它总是返回一个Object而不是图片。指导我正确的方向

非常感谢您提前

1 个答案:

答案 0 :(得分:1)

尝试Pygame。它不仅可以让您显示图像,还可以帮助您移动它们并与它们进行交互。如果你打算下棋,我相信Pygame会有所帮助!如果你真的想坚持SVG那么......我想我只是想帮忙。 如果你想要一个示例代码,请在下面评论。希望这有帮助!

好的,我完成了。到目前为止,游戏允许您将棋子移动到允许的位置。但其余的我会留给你,因为这需要一点时间。如果您在pygame中需要任何指导,请再次发表评论,我会尽力提供帮助。这是代码:

import pygame, time, sys, random
from pygame.locals import *

# Initialize pygame (this is nessseccary for it to run all it's commands)
pygame.init()

# Draw the screen. The width and the height define how long and how wide the window is
width = 600
height = 500
window = pygame.display.set_mode((width, height))
TRANSwindow = window.convert_alpha()

# this sets the window title to "Chess!"
pygame.display.set_caption("Chess!")

# define some colours that we might need for later decoration
aqua = 0, 255, 255
black = 0, 0, 0
blue = 0, 0, 255
fuchsia = 255, 0, 255
gray = 128, 128, 128
green = 0, 128, 0
lime = 0, 255, 0
maroon = 128, 0, 0
navy_blue = 0, 0, 128
olive = 128, 128, 0
purple = 128, 0, 128
red = 255, 0, 0
silver = 192, 192, 192
teal = 0, 128, 128
white = 255, 255, 255
yellow = 255, 255, 0


# Make a group that contains the sprite pieces that we need
pieces = pygame.sprite.Group()

# Make a group that contains the grid called "board" that we need
grid = pygame.sprite.Group()

# Make a group that contains the allowed click spaces called "ClickSpaces" that we need
ClickSpaces = pygame.sprite.Group()

class Board(pygame.sprite.Sprite):
    def __init__(self):
        super().__init__()
        self.image = pygame.image.load("yoHZB.png")
        self.image = pygame.transform.scale(self.image,(600,500))
        self.rect = self.image.get_rect()

class Piece(pygame.sprite.Sprite):
    def __init__(self):
        super().__init__()
        self.image = pygame.image.load("Hr7mM.png")
        self.image = pygame.transform.scale(self.image,(75,65))
        self.rect = self.image.get_rect()
class ClickSpace(pygame.sprite.Sprite):
    def __init__(self):
        super().__init__()
        self.image = pygame.image.load("I6rbE.png")
        self.image = pygame.transform.scale(self.image, (75, 65))
        self.rect = self.image.get_rect()

for i in range(8):
    piece = Piece()
    piece.rect.x = i*75
    piece.rect.y = 0
    pieces.add(piece)
for i in range(8):
    piece = Piece()
    piece.rect.x = i*75
    piece.rect.y = 65
    pieces.add(piece)
for i in range(8):
    piece = Piece()
    piece.rect.x = i*75
    piece.rect.y = 380
    pieces.add(piece)
for i in range(8):
    piece = Piece()
    piece.rect.x = i*75
    piece.rect.y = 440
    pieces.add(piece)

board = Board()
grid.add(board)


while True:
    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            exit()
        elif event.type == MOUSEBUTTONUP:
            mouse = pygame.mouse.get_pos()
            for piece in pieces:
                if piece.rect.collidepoint(mouse):
                    print("clicked")
                    for i in range(2):
                        clickspace = ClickSpace()
                        clickspace.rect.x = piece.rect.left
                        clickspace.rect.y = piece.rect.top+((i+1)*65)
                        ClickSpaces.add(clickspace)
                        print("added clickspace")
                    ClickSpaces.draw(window)
                    pygame.display.update()
                    wait = True
                    while wait == True:
                        mouse = pygame.mouse.get_pos()
                        for event in pygame.event.get():
                            if event.type == QUIT:
                                pygame.quit()
                                exit()
                            elif event.type == MOUSEBUTTONUP:
                                mouse = pygame.mouse.get_pos()
                                for clickspace in ClickSpaces:
                                    if clickspace.rect.collidepoint(mouse):
                                        print("clickspace clicked")
                                        piece.rect.x = clickspace.rect.left
                                        piece.rect.y = clickspace.rect.top
                                        ClickSpaces.empty()
                                        wait = False
                                    else:
                                        ClickSpaces.empty()
                                        wait = False


    window.fill(white)
    grid.draw(window)
    pieces.draw(window)
    ClickSpaces.draw(window)
    pygame.display.update()

复制代码并将其放入python后。保存项目,然后下载以下图像并将它们放在与代码相同的文件中:

enter image description here

enter image description here

enter image description here