Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/19.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 返回SVG对象或XML字符串而不是图像_Python_Python 3.x_Svg - Fatal编程技术网

Python 返回SVG对象或XML字符串而不是图像

Python 返回SVG对象或XML字符串而不是图像,python,python-3.x,svg,Python,Python 3.x,Svg,嘿,伙计们,我是一个初级程序员,在这个特殊的应用程序中遇到了麻烦 我想从python象棋库中呈现一个棋盘,它总是返回一个SVG xml字符串而不是图像。这是文件 >chess.svg.piece(chess.piece.from_符号(“R”)) '' 我曾尝试使用svgwrite渲染此内容,但无法完成 在python中是否有其他方法可以直接将所述XML呈现为图像并显示在屏幕上 还尝试使用IPython.display模块尝试显示SVG,但它总是返回一个对象而不是图片。指引我正确的方向 提前非

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

我想从python象棋库中呈现一个棋盘,它总是返回一个SVG xml字符串而不是图像。这是文件

>chess.svg.piece(chess.piece.from_符号(“R”))
''
我曾尝试使用svgwrite渲染此内容,但无法完成

在python中是否有其他方法可以直接将所述XML呈现为图像并显示在屏幕上

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


提前非常感谢您

试试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之后。保存项目,然后下载以下图像并将其与代码放在同一文件中:


试试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之后。保存项目,然后下载以下图像并将其与代码放在同一文件中:


是的,国际象棋是我想做的。当然可以使用pygame的一些示例代码。顺便问一下,您有pygame模块吗?如果没有它,这将无法工作。是的,我以前使用过Pygame,我一定会尝试!是的,国际象棋是我想做的。当然可以使用pygame的一些示例代码。顺便问一下,您有pygame模块吗?如果没有它,这将无法工作。是的,我以前使用过Pygame,我一定会尝试!您的问题应该提到您正在使用的GUI框架。FWIW,这里有一个在pygame中呈现SVG的示例。请注意,它使用了两个其他库来实际处理SVG数据。FWIW,您可以在浏览器中显示SVG。如果SVG数据是一个名为
SVG\u data
的字符串,它将在FireFox中显示为数据URI
import base64,webbrowser
webbrowser.get('firefox').open('data:image/svg+xml;base64,'+base64.encodestring(svg_data.encode()).decode())
您的问题应该提到您使用的GUI框架。FWIW,这里有一个在pygame中呈现SVG的示例。请注意,它使用了两个其他库来实际处理SVG数据。FWIW,您可以在浏览器中显示SVG。如果SVG数据是一个名为
SVG\u data
的字符串,它将在FireFox中显示为数据URI
import base64,webbrowser
webbrowser.get('firefox').open('data:image/svg+xml;base64'+base64.encodestring(svg_data.encode()).decode())