From 5af7abb75a87146e58f9a4180cbbca630e1c705d Mon Sep 17 00:00:00 2001 From: Brady James Garvin <bgarvin@cse.unl.edu> Date: Mon, 26 Jul 2021 11:08:46 -0500 Subject: [PATCH] Prefer sending data from the view over retrieving it from the controller. --- main.py | 15 +++++++++++++-- triangle.kv | 15 ++++++--------- 2 files changed, 19 insertions(+), 11 deletions(-) diff --git a/main.py b/main.py index 4990bdc..adcd1c8 100644 --- a/main.py +++ b/main.py @@ -13,6 +13,9 @@ class Classification(Enum): class TriangleApp(App): + a = StringProperty('') + b = StringProperty('') + c = StringProperty('') classification = StringProperty() @staticmethod @@ -36,15 +39,23 @@ class TriangleApp(App): return Classification.SCALENE def update(self): - ids = self.root.ids try: - self.classification = TriangleApp.classify(int(ids.a.text), int(ids.b.text), int(ids.c.text)).value + self.classification = TriangleApp.classify(int(self.a), int(self.b), int(self.c)).value except ValueError: self.classification = f'{Classification.INVALID.value} (non-integer sides)' def on_start(self): self.update() + def on_a(self, _, __): + self.update() + + def on_b(self, _, __): + self.update() + + def on_c(self, _, __): + self.update() + if __name__ == '__main__': app = TriangleApp() diff --git a/triangle.kv b/triangle.kv index f9de87b..8468ab8 100644 --- a/triangle.kv +++ b/triangle.kv @@ -13,12 +13,11 @@ BoxLayout: Widget: size_hint: (0.25, 1.0) TextInput: - id: a multiline: False write_tab: False - text: '' + text: app.a font_size: sp(24) - on_text: app.update() + on_text: app.a = self.text Widget: BoxLayout: orientation: 'horizontal' @@ -31,12 +30,11 @@ BoxLayout: Widget: size_hint: (0.25, 1.0) TextInput: - id: b multiline: False write_tab: False - text: '' + text: app.b font_size: sp(24) - on_text: app.update() + on_text: app.b = self.text Widget: BoxLayout: orientation: 'horizontal' @@ -49,12 +47,11 @@ BoxLayout: Widget: size_hint: (0.25, 1.0) TextInput: - id: c multiline: False write_tab: False - text: '' + text: app.c font_size: sp(24) - on_text: app.update() + on_text: app.c = self.text Widget: Widget: BoxLayout: -- GitLab