diff --git a/first_screen.kv b/first_screen.kv index 08e2856432e9ff2bedf50f872ac722bcc5e88f4c..a1120fb9e45e596eff91e227bd0798fa400e5913 100644 --- a/first_screen.kv +++ b/first_screen.kv @@ -6,6 +6,9 @@ Button: text: 'This button prints a number.' on_press: root.print_number() # In a custom widget's Kv, `root` refers to the custom widget. + Button: + text: 'This button rolls a new number.' + on_press: app.reroll() Button: text: 'Change…' on_press: app.open_second_screen() # But `app` still refers to the application object. diff --git a/main.py b/main.py index 27813546f18d74b099ced74e59e6ca5fbf5bd702..6dbd102872a1e6c44c75eee3ec805f64d66de816 100644 --- a/main.py +++ b/main.py @@ -1,5 +1,7 @@ +from random import randrange + from kivy.app import App -from kivy.properties import BooleanProperty, StringProperty +from kivy.properties import BooleanProperty, NumericProperty, StringProperty # The following lines ensure that FirstScreen and SecondScreen are available for when the Kv code is loaded. @@ -16,6 +18,8 @@ class TwoScreenApp(App): advancing = BooleanProperty(True) current_screen_name = StringProperty() # We must wait to select a screen until the app starts. + roll = NumericProperty(1) + def open_first_screen(self): self.advancing = True self.current_screen_name = 'first' @@ -24,6 +28,12 @@ class TwoScreenApp(App): self.advancing = False self.current_screen_name = 'second' + def reroll(self): + new_roll = randrange(1, 6) + if new_roll >= self.roll: + new_roll +=1 + self.roll = new_roll + def on_start(self): self.open_first_screen() # Once the app starts, we can select the starting screen. diff --git a/second_screen.kv b/second_screen.kv index 7fcb6f5f3e0a1b55c067f7a6f5f2f229b0b8f8d8..930236934b1dc51671f1a824a71219bd66a7a36c 100644 --- a/second_screen.kv +++ b/second_screen.kv @@ -5,6 +5,9 @@ text: 'This is the second screen.' Label: text: root.message # In a custom widget's Kv, `root` refers to the custom widget. + Button: + text: 'This button prints the roll from the first screen.' + on_press: root.print_roll() Button: text: 'Change…' on_press: app.open_first_screen() # But `app` still refers to the application object. diff --git a/second_screen.py b/second_screen.py index 7521f7122e3ada56f8aed7bc1b20cb69a1917b39..50cb1b9403e252819b45b6014cb3b9d3be8f2d9b 100644 --- a/second_screen.py +++ b/second_screen.py @@ -1,6 +1,6 @@ from kivy.lang import Builder from kivy.uix.screenmanager import Screen -from kivy.properties import StringProperty +from kivy.properties import NumericProperty, StringProperty Builder.load_file('second_screen.kv') # Because this is not the main app, we must load the Kv explicitly. @@ -8,3 +8,7 @@ Builder.load_file('second_screen.kv') # Because this is not the main app, we mu class SecondScreen(Screen): message = StringProperty('Hello, World!') # This property is changed by twoscreen.kv. + roll = NumericProperty() + + def print_roll(self): + print(f'The latest roll was a {self.roll}.') diff --git a/twoscreen.kv b/twoscreen.kv index 331e11125fab04ad80a3bfce63e38777258d9627..53ca161f3da3387796db20be0694ea502dd704d2 100644 --- a/twoscreen.kv +++ b/twoscreen.kv @@ -11,3 +11,4 @@ ScreenManager: SecondScreen: name: 'second' message: 'This is a message!' # The app sends information to the custom widgets by setting their properties. + roll: app.roll