如何在kivy文件中使用“mesh”而不是python文件

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

我想在python中使用“mesh”绘制一个自定义形状与kivy。我对此做了一些研究,但大部分结果只是在python文件中编写代码

来自herehere的代码显示了在python文件中构造网格对象的方法,但是当我尝试将其转换为kivy文件时,我发现了一个问题

这是我的主文件(main.py)中的代码:

from kivy.lang import Builder
from kivy.uix.screenmanager import Screen
from kivy.graphics import Mesh
from kivy.properties import ObjectProperty

class MainScreen(Screen):
    Mesh = ObjectProperty(None)

class TestApp(App):
    def build(self):
        return Builder.load_file("health.kv")

sample_app = TestApp()
sample_app.run()

这是我的kivy文件(test.kv)中的代码:

<MainScreen>:
    name: "main"
    Mesh:
        vertices: [0, 0, 0, 0, 100, 0, 0, 0, 100, 100, 0, 0]
        indices: [0, 1, 2]

错误如下:

File "C:\Users\kelv1\AppData\Local\Programs\Python\Python37-32\lib\site-packages\kivy\uix\floatlayout.py", line 135, in add_widget
 widget.bind(
AttributeError: 'kivy.graphics.vertex_instructions.Mesh' object has no attribute 'bind'

为什么会发生这种情况以及如何解决?

python-3.x kivy mesh
1个回答
0
投票

你只是忘了把它放在画布上。我添加了triangle_fan模式,使其成为多边形。只是猜测这就是你想要的。 试试这个:

from kivy.app import App
from kivy.lang import Builder

KV = """

<MainScreen@Screen>:
    name: "main"
    canvas:
        Mesh:
            mode: "triangle_fan"
            vertices: [0, 0, 0, 0, 100, 0, 0, 0, 100, 100, 0, 0]
            indices: [0, 1, 2]

MainScreen:

"""


class MyApp(App):

    def build(self):
        return Builder.load_string(KV)


MyApp().run()
© www.soinside.com 2019 - 2024. All rights reserved.