在Kivy Python中显示SVG文件中的棋盘

时间:2019-06-20 10:46:31

标签: python svg kivy

我正在尝试显示带有svg文件中的棋子的棋盘,但结果不是我所期望的。棋盘印制时没有碎片。这些作品出现在一个角落的顶部。如果我在Jupyter Notebook中打印svg文件,则显示板正确。

我的Kivy应用程序的输出

Output from my script

Jupyter Notebook的输出

Output from Jupyter Notebook

我使用macOS Mojave,python 3.7和kivy 1.11。

Python脚本

from kivy.uix.scatter import Scatter
from kivy.app import App
from kivy.graphics.svg import Svg
from kivy.uix.floatlayout import FloatLayout
from kivy.lang import Builder
#from IPython.display import SVG

Builder.load_string("""
<SvgWidget>:
    do_rotation: False
<FloatLayout>:
    canvas.before:
        Color:
            rgb: (1, 1, 1)
        Rectangle:
            pos: self.pos
            size: self.size
""")

#SVG(filename="../testchess/boards/board_0.1.7.svg") 
class SvgWidget(Scatter):

    def __init__(self, filename, **kwargs):
        super(SvgWidget, self).__init__(**kwargs)
        with self.canvas:
            svg = Svg(filename)
        self.size = svg.width, svg.height


class SvgApp(App):

    def build(self):
        self.root = FloatLayout()

        filename = "../testchess/boards/board_0.1.7.svg"
        svg = SvgWidget(filename, size_hint=(None, None), pos_hint={'center_x': 0.5, 'top': 1})
        self.root.add_widget(svg)
        svg.scale = 2


if __name__ == '__main__':
    SvgApp().run()

svg

<svg version="1.1" viewBox="0 0 400 400" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"><defs><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><g class="white king" fill="none" fill-rule="evenodd" id="white-king" stroke="#000" stroke-linecap="round" stroke-linejoin="round" stroke-width="1.5"><path d="M22.5 11.63V6M20 8h5" stroke-linejoin="miter" /><path d="M22.5 25s4.5-7.5 3-10.5c0 0-1-2.5-3-2.5s-3 2.5-3 2.5c-1.5 3 3 10.5 3 10.5" fill="#fff" stroke-linecap="butt" stroke-linejoin="miter" /><path d="M11.5 37c5.5 3.5 15.5 3.5 21 0v-7s9-4.5 6-10.5c-4-6.5-13.5-3.5-16 4V27v-3.5c-3.5-7.5-13-10.5-16-4-3 6 5 10 5 10V37z" fill="#fff" /><path d="M11.5 30c5.5-3 15.5-3 21 0m-21 3.5c5.5-3 15.5-3 21 0m-21 3.5c5.5-3 15.5-3 21 0" /></g></defs><rect class="square dark a1" fill="#d18b47" height="45" stroke="none" width="45" x="20" y="335" /><use transform="translate(20, 335)" xlink:href="#white-rook" /><rect class="square light b1" fill="#ffce9e" height="45" stroke="none" width="45" x="65" y="335" /><rect class="square dark c1" fill="#d18b47" height="45" stroke="none" width="45" x="110" y="335" /><rect class="square light d1" fill="#ffce9e" height="45" stroke="none" width="45" x="155" y="335" /><rect class="square dark e1" fill="#d18b47" height="45" stroke="none" width="45" x="200" y="335" /><use transform="translate(200, 335)" xlink:href="#white-king" /><rect class="square light f1" fill="#ffce9e" height="45" stroke="none" width="45" x="245" y="335" /><rect class="square dark g1" fill="#d18b47" height="45" stroke="none" width="45" x="290" y="335" /><rect class="square light h1" fill="#ffce9e" height="45" stroke="none" width="45" x="335" y="335" /><use transform="translate(335, 335)" xlink:href="#white-rook" /><rect class="square light a2" fill="#ffce9e" height="45" stroke="none" width="45" x="20" y="290" /><rect class="square dark b2" fill="#d18b47" height="45" stroke="none" width="45" x="65" y="290" /><rect class="square light c2" fill="#ffce9e" height="45" stroke="none" width="45" x="110" y="290" /><rect class="square dark d2" fill="#d18b47" height="45" stroke="none" width="45" x="155" y="290" /><rect class="square light e2" fill="#ffce9e" height="45" stroke="none" width="45" x="200" y="290" /><rect class="square dark f2" fill="#d18b47" height="45" stroke="none" width="45" x="245" y="290" /><rect class="square light g2" fill="#ffce9e" height="45" stroke="none" width="45" x="290" y="290" /><rect class="square dark h2" fill="#d18b47" height="45" stroke="none" width="45" x="335" y="290" /><rect class="square dark a3" fill="#d18b47" height="45" stroke="none" width="45" x="20" y="245" /><rect class="square light b3" fill="#ffce9e" height="45" stroke="none" width="45" x="65" y="245" /><rect class="square dark c3" fill="#d18b47" height="45" stroke="none" width="45" x="110" y="245" /><rect class="square light d3" fill="#ffce9e" height="45" stroke="none" width="45" x="155" y="245" /><rect class="square dark e3" fill="#d18b47" height="45" stroke="none" width="45" x="200" y="245" /><rect class="square light f3" fill="#ffce9e" height="45" stroke="none" width="45" x="245" y="245" /><rect class="square dark g3" fill="#d18b47" height="45" stroke="none" width="45" x="290" y="245" /><rect class="square light h3" fill="#ffce9e" height="45" stroke="none" width="45" x="335" y="245" /><rect class="square light a4" fill="#ffce9e" height="45" stroke="none" width="45" x="20" y="200" /><rect class="square dark b4" fill="#d18b47" height="45" stroke="none" width="45" x="65" y="200" /><rect class="square light c4" fill="#ffce9e" height="45" stroke="none" width="45" x="110" y="200" /><rect class="square dark d4" fill="#d18b47" height="45" stroke="none" width="45" x="155" y="200" /><rect class="square light e4" fill="#ffce9e" height="45" stroke="none" width="45" x="200" y="200" /><rect class="square dark f4" fill="#d18b47" height="45" stroke="none" width="45" x="245" y="200" /><rect class="square light g4" fill="#ffce9e" height="45" stroke="none" width="45" x="290" y="200" /><rect class="square dark h4" fill="#d18b47" height="45" stroke="none" width="45" x="335" y="200" /><rect class="square dark a5" fill="#d18b47" height="45" stroke="none" width="45" x="20" y="155" /><rect class="square light b5" fill="#ffce9e" height="45" stroke="none" width="45" x="65" y="155" /><rect class="square dark c5" fill="#d18b47" height="45" stroke="none" width="45" x="110" y="155" /><rect class="square light d5" fill="#ffce9e" height="45" stroke="none" width="45" x="155" y="155" /><rect class="square dark e5" fill="#d18b47" height="45" stroke="none" width="45" x="200" y="155" /><rect class="square light f5" fill="#ffce9e" height="45" stroke="none" width="45" x="245" y="155" /><rect class="square dark g5" fill="#d18b47" height="45" stroke="none" width="45" x="290" y="155" /><rect class="square light h5" fill="#ffce9e" height="45" stroke="none" width="45" x="335" y="155" /><rect class="square light a6" fill="#ffce9e" height="45" stroke="none" width="45" x="20" y="110" /><rect class="square dark b6" fill="#d18b47" height="45" stroke="none" width="45" x="65" y="110" /><rect class="square light c6" fill="#ffce9e" height="45" stroke="none" width="45" x="110" y="110" /><rect class="square dark d6" fill="#d18b47" height="45" stroke="none" width="45" x="155" y="110" /><rect class="square light e6" fill="#ffce9e" height="45" stroke="none" width="45" x="200" y="110" /><rect class="square dark f6" fill="#d18b47" height="45" stroke="none" width="45" x="245" y="110" /><rect class="square light g6" fill="#ffce9e" height="45" stroke="none" width="45" x="290" y="110" /><rect class="square dark h6" fill="#d18b47" height="45" stroke="none" width="45" x="335" y="110" /><rect class="square dark a7" fill="#d18b47" height="45" stroke="none" width="45" x="20" y="65" /><rect class="square light b7" fill="#ffce9e" height="45" stroke="none" width="45" x="65" y="65" /><rect class="square dark c7" fill="#d18b47" height="45" stroke="none" width="45" x="110" y="65" /><rect class="square light d7" fill="#ffce9e" height="45" stroke="none" width="45" x="155" y="65" /><rect class="square dark e7" fill="#d18b47" height="45" stroke="none" width="45" x="200" y="65" /><rect class="square light f7" fill="#ffce9e" height="45" stroke="none" width="45" x="245" y="65" /><rect class="square dark g7" fill="#d18b47" height="45" stroke="none" width="45" x="290" y="65" /><rect class="square light h7" fill="#ffce9e" height="45" stroke="none" width="45" x="335" y="65" /><rect class="square light a8" fill="#ffce9e" height="45" stroke="none" width="45" x="20" y="20" /><rect class="square dark b8" fill="#d18b47" height="45" stroke="none" width="45" x="65" y="20" /><rect class="square light c8" fill="#ffce9e" height="45" stroke="none" width="45" x="110" y="20" /><rect class="square dark d8" fill="#d18b47" height="45" stroke="none" width="45" x="155" y="20" /><rect class="square light e8" fill="#ffce9e" height="45" stroke="none" width="45" x="200" y="20" /><rect class="square dark f8" fill="#d18b47" height="45" stroke="none" width="45" x="245" y="20" /><rect class="square light g8" fill="#ffce9e" height="45" stroke="none" width="45" x="290" y="20" /><rect class="square dark h8" fill="#d18b47" height="45" stroke="none" width="45" x="335" y="20" /><text alignment-baseline="middle" font-size="14" text-anchor="middle" x="42" y="10">a</text><text alignment-baseline="middle" font-size="14" text-anchor="middle" x="42" y="390">a</text><text alignment-baseline="middle" font-size="14" text-anchor="middle" x="87" y="10">b</text><text alignment-baseline="middle" font-size="14" text-anchor="middle" x="87" y="390">b</text><text alignment-baseline="middle" font-size="14" text-anchor="middle" x="132" y="10">c</text><text alignment-baseline="middle" font-size="14" text-anchor="middle" x="132" y="390">c</text><text alignment-baseline="middle" font-size="14" text-anchor="middle" x="177" y="10">d</text><text alignment-baseline="middle" font-size="14" text-anchor="middle" x="177" y="390">d</text><text alignment-baseline="middle" font-size="14" text-anchor="middle" x="222" y="10">e</text><text alignment-baseline="middle" font-size="14" text-anchor="middle" x="222" y="390">e</text><text alignment-baseline="middle" font-size="14" text-anchor="middle" x="267" y="10">f</text><text alignment-baseline="middle" font-size="14" text-anchor="middle" x="267" y="390">f</text><text alignment-baseline="middle" font-size="14" text-anchor="middle" x="312" y="10">g</text><text alignment-baseline="middle" font-size="14" text-anchor="middle" x="312" y="390">g</text><text alignment-baseline="middle" font-size="14" text-anchor="middle" x="357" y="10">h</text><text alignment-baseline="middle" font-size="14" text-anchor="middle" x="357" y="390">h</text><text alignment-baseline="middle" font-size="14" text-anchor="middle" x="10" y="357">1</text><text alignment-baseline="middle" font-size="14" text-anchor="middle" x="390" y="357">1</text><text alignment-baseline="middle" font-size="14" text-anchor="middle" x="10" y="312">2</text><text alignment-baseline="middle" font-size="14" text-anchor="middle" x="390" y="312">2</text><text alignment-baseline="middle" font-size="14" text-anchor="middle" x="10" y="267">3</text><text alignment-baseline="middle" font-size="14" text-anchor="middle" x="390" y="267">3</text><text alignment-baseline="middle" font-size="14" text-anchor="middle" x="10" y="222">4</text><text alignment-baseline="middle" font-size="14" text-anchor="middle" x="390" y="222">4</text><text alignment-baseline="middle" font-size="14" text-anchor="middle" x="10" y="177">5</text><text alignment-baseline="middle" font-size="14" text-anchor="middle" x="390" y="177">5</text><text alignment-baseline="middle" font-size="14" text-anchor="middle" x="10" y="132">6</text><text alignment-baseline="middle" font-size="14" text-anchor="middle" x="390" y="132">6</text><text alignment-baseline="middle" font-size="14" text-anchor="middle" x="10" y="87">7</text><text alignment-baseline="middle" font-size="14" text-anchor="middle" x="390" y="87">7</text><text alignment-baseline="middle" font-size="14" text-anchor="middle" x="10" y="42">8</text><text alignment-baseline="middle" font-size="14" text-anchor="middle" x="390" y="42">8</text></svg>

我希望棋盘上的棋子就位。

0 个答案:

没有答案