【问题标题】:Centering Tesselator Shape in Kivy在 Kivy 中居中 Tesselator 形状
【发布时间】:2020-09-03 17:37:21
【问题描述】:

我在 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()

【问题讨论】:

    标签: python layout kivy kivy-language


    【解决方案1】:

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

    #:kivy 1.11.1
    
    FloatLayout:
        MapBackground:
            on_size: self.draw()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-12-28
      • 1970-01-01
      • 1970-01-01
      • 2019-10-08
      • 2013-12-25
      • 1970-01-01
      • 2014-02-13
      • 2013-12-25
      相关资源
      最近更新 更多