将猕猴桃中的Tesselator形状居中

时间:2020-05-18 00:57:41

标签: python layout kivy kivy-language

好吧,我在kivy中使用了称为“ Tesselator(https://kivy.org/doc/stable/api-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()

1 个答案:

答案 0 :(得分:0)

通过将绘制形状的小部件放置在浮动布局中,我做了我想要的事情。然后(使用下面显示的kv文件)我将一个名为draw()的方法绑定到事件on_size。这意味着只要窗口更改形状,形状就会重新绘制。我不会显示draw()的代码,因为它很多。

#:kivy 1.11.1

FloatLayout:
    MapBackground:
        on_size: self.draw()