我想在画布中绘制几个三角形,因为我调整窗口的大小时会调整大小。代码使用如下

from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.label import  Label
from kivy.lang import Builder
from kivy.uix.textinput import TextInput
from kivy.uix.stacklayout import StackLayout
from kivy.uix.button import Button
from kivy.uix.gridlayout import GridLayout

    Builder.load_string("""

    <ScreenUI>:
        radius: 0.9 * min(self.center_x, self.center_y)
        tside: 2 * (min(self.center_x, self.center_y) - self.radius / 1.4)
        r_width: self.center_x + self.radius
        r_x: self.center_x - self.radius

        canvas:
            Triangle:
                points: root.r_x, 0, root.tside, 0, 0, root.tside
            Triangle:
                points: root.r_width, 0, root.r_x - root.tside, 0, root.r_width, root.tside
    """)

    class ScreenUI(BoxLayout):
        pass

    class WidgetApp(App): 

        def build(self):
            app = ScreenUI()
            return app

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

我得到的错误是:

kivy.lang.BuilderException: Parser: File "<inline>", line 13:
...
  11:            points: root.r_x, 0, root.tside, 0, 0, root.tside
  12:        Triangle:
>>13:            points: root.r_width, 0, root.r_x - root.tside, 0, root.r_width,   root.tside
...
BuilderException: Parser: File "<inline>", line 13:
...
  11:            points: root.r_x, 0, root.tside, 0, 0, root.tside
  12:        Triangle:
>>13:            points: root.r_width, 0, root.r_x - root.tside, 0, root.r_width, root.tside
 ...
 TypeError: unsupported operand type(s) for -: 'NoneType' and 'NoneType'
.

如何使用KV语言来计算三角形的角落?

有帮助吗?

解决方案

您需要将属性添加到ScreenUI python类:

class ScreenUI(BoxLayout):
    radius = NumericProperty(0)
    tside = NumericProperty(0)
    r_width = NumericProperty(0)
    r_x = NumericProperty(0)
.

别忘了导入NumericProperty

from kivy.properties import NumericProperty
.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top