在Kivy中的居中Tesselator形状。

问题描述 投票:1回答:1

我在kivy中使用了一个叫做Tesselator的 "实验性 "功能(https:/kivy.orgdocstableapi-kivy.graphics.tesselator.html。). 目前我的应用程序在左下角画了一个小形状。它没有使用.kv文件。

我想知道,当你改变窗口的大小时,如何使形状在屏幕上保持居中?我已经看到了如何用基本形状来实现这个功能,但我不知道如何用棋盘形状来实现这个功能。

任何建议和意见都非常感激 代码如下。

import kivy
from kivy.app import App
from kivy.app import App
from kivy.graphics import Mesh
from kivy.graphics.tesselator import Tesselator
from kivy.uix.floatlayout import FloatLayout

class MapBackground(FloatLayout):
    def __init__(self, **kwargs):
        super(MapBackground, self).__init__(**kwargs)
        self.shapes = [
            [100, 100, 300, 100, 300, 300, 100, 300],
            [150, 150, 250, 150, 250, 250, 150, 250]
        ]        
        self.draw()

    # draws the map shape
    def draw(self):
        # make the tesselator object
        tess = Tesselator()
        # add all the shapes
        for shape in self.shapes:
            if len(shape) >= 3:
                tess.add_contour(shape)
        # call the tesselate method to compute the points 
        if not tess.tesselate(): # returns false if doesn't work
            print('tesselator did\'t work:(')
            return
        # clear canvas
        self.canvas.clear()
        # draw shapes
        for vertices, indices in tess.meshes:
            self.canvas.add(Mesh(
                vertices=vertices,
                indices=indices,
                mode='triangle_fan'
            ))

class TimmApp(App):
    def build(self):
        self.title = 'The Interactive Museum Map'
        return MapBackground()

if __name__ == '__main__':
    TimmApp().run()
python layout kivy kivy-language
1个回答
0
投票

我做了我想做的事情,把绘制形状的小组件放在浮动布局中。然后(使用下面看到的kv文件)我将一个叫做draw()的方法绑定到on_size事件上。这意味着每当窗口改变形状时,形状就会被重新绘制。我不会显示draw()的代码,因为它有很多。

#:kivy 1.11.1

FloatLayout:
    MapBackground:
        on_size: self.draw()
© www.soinside.com 2019 - 2024. All rights reserved.