From f18f6b6d6df2aad02cd6045a78f9d84f9bc1ad80 Mon Sep 17 00:00:00 2001
From: Brady James Garvin <bgarvin@cse.unl.edu>
Date: Sun, 18 Jul 2021 12:12:40 -0500
Subject: [PATCH] Used model properties to control the ScreenManager, moving
 presentation logic to the Kv.

---
 main.py      | 15 +++++++++++----
 twoscreen.kv |  9 +++++++--
 2 files changed, 18 insertions(+), 6 deletions(-)

diff --git a/main.py b/main.py
index b7ebfae..2781354 100644
--- a/main.py
+++ b/main.py
@@ -1,4 +1,5 @@
 from kivy.app import App
+from kivy.properties import BooleanProperty, StringProperty
 
 # The following lines ensure that FirstScreen and SecondScreen are available for when the Kv code is loaded.
 
@@ -12,13 +13,19 @@ from second_screen import SecondScreen  # pylint: disable=unused-import
 
 
 class TwoScreenApp(App):
+    advancing = BooleanProperty(True)
+    current_screen_name = StringProperty()  # We must wait to select a screen until the app starts.
+
     def open_first_screen(self):
-        self.root.transition.direction = 'right'
-        self.root.current = 'first'
+        self.advancing = True
+        self.current_screen_name = 'first'
 
     def open_second_screen(self):
-        self.root.transition.direction = 'left'
-        self.root.current = 'second'
+        self.advancing = False
+        self.current_screen_name = 'second'
+
+    def on_start(self):
+        self.open_first_screen()  # Once the app starts, we can select the starting screen.
 
 
 if __name__ == '__main__':
diff --git a/twoscreen.kv b/twoscreen.kv
index ca47926..331e111 100644
--- a/twoscreen.kv
+++ b/twoscreen.kv
@@ -1,8 +1,13 @@
+# We import the type of transition we want so that we can use it in the `transition` property below.
+# Imports have three parts: #:import, the name we want to use in our code, and the fully qualified name.
+
+#:import SlideTransition kivy.uix.screenmanager.SlideTransition
+
 ScreenManager:
+    transition: SlideTransition(direction='right' if app.advancing else 'left')
+    current: app.current_screen_name
     FirstScreen:
-        id: first
         name: 'first'
     SecondScreen:
-        id: second
         name: 'second'
         message: 'This is a message!'  # The app sends information to the custom widgets by setting their properties.
-- 
GitLab