使用Kivy的KV语言在画布上计算点

时间:2013-11-16 21:07:17

标签: layout kivy

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

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语言计算三角形的角落?

1 个答案:

答案 0 :(得分:1)

您需要将属性添加到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