From d1c9173c0e494067e997a781fa0ef6dc1abf1c4b Mon Sep 17 00:00:00 2001 From: Brady James Garvin <bgarvin@cse.unl.edu> Date: Mon, 26 Jul 2021 21:28:44 -0500 Subject: [PATCH] Show how to makee data computed and stored in the main app class available to a screen. --- first_screen.kv | 3 +++ main.py | 12 +++++++++++- second_screen.kv | 3 +++ second_screen.py | 6 +++++- twoscreen.kv | 1 + 5 files changed, 23 insertions(+), 2 deletions(-) diff --git a/first_screen.kv b/first_screen.kv index 08e2856..a1120fb 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 2781354..6dbd102 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 7fcb6f5..9302369 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 7521f71..50cb1b9 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 331e111..53ca161 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 -- GitLab