Verwenden der KV-Sprache von Kivy zum Berechnen von Punkten auf einer Leinwand
Frage
Ich möchte ein paar Dreiecke auf einer Leinwand zeichnen, deren Größe sich anpasst, wenn ich die Fenstergröße verändere.Die Codeverwendung ist wie folgt
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()
Die Fehler, die ich bekomme, sind:
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'
Wie kann ich nur die KV-Sprache verwenden, um alle Ecken der Dreiecke zu berechnen?
Lösung
Sie müssen die Eigenschaften zum hinzufügen ScreenUI
Python-Klasse:
class ScreenUI(BoxLayout):
radius = NumericProperty(0)
tside = NumericProperty(0)
r_width = NumericProperty(0)
r_x = NumericProperty(0)
Vergessen Sie nicht, das zu importieren NumericProperty
:
from kivy.properties import NumericProperty
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow