Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
A
App Data in Separate Screens
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Brady James Garvin
App Data in Separate Screens
Commits
d1c9173c
Commit
d1c9173c
authored
3 years ago
by
Brady James Garvin
Browse files
Options
Downloads
Patches
Plain Diff
Show how to makee data computed and stored in the main app class available to a screen.
parent
f18f6b6d
No related branches found
No related tags found
No related merge requests found
Changes
5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
first_screen.kv
+3
-0
3 additions, 0 deletions
first_screen.kv
main.py
+11
-1
11 additions, 1 deletion
main.py
second_screen.kv
+3
-0
3 additions, 0 deletions
second_screen.kv
second_screen.py
+5
-1
5 additions, 1 deletion
second_screen.py
twoscreen.kv
+1
-0
1 addition, 0 deletions
twoscreen.kv
with
23 additions
and
2 deletions
first_screen.kv
+
3
−
0
View file @
d1c9173c
...
@@ -6,6 +6,9 @@
...
@@ -6,6 +6,9 @@
Button:
Button:
text: 'This button prints a number.'
text: 'This button prints a number.'
on_press: root.print_number() # In a custom widget's Kv, `root` refers to the custom widget.
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:
Button:
text: 'Change…'
text: 'Change…'
on_press: app.open_second_screen() # But `app` still refers to the application object.
on_press: app.open_second_screen() # But `app` still refers to the application object.
This diff is collapsed.
Click to expand it.
main.py
+
11
−
1
View file @
d1c9173c
from
random
import
randrange
from
kivy.app
import
App
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.
# The following lines ensure that FirstScreen and SecondScreen are available for when the Kv code is loaded.
...
@@ -16,6 +18,8 @@ class TwoScreenApp(App):
...
@@ -16,6 +18,8 @@ class TwoScreenApp(App):
advancing
=
BooleanProperty
(
True
)
advancing
=
BooleanProperty
(
True
)
current_screen_name
=
StringProperty
()
# We must wait to select a screen until the app starts.
current_screen_name
=
StringProperty
()
# We must wait to select a screen until the app starts.
roll
=
NumericProperty
(
1
)
def
open_first_screen
(
self
):
def
open_first_screen
(
self
):
self
.
advancing
=
True
self
.
advancing
=
True
self
.
current_screen_name
=
'
first
'
self
.
current_screen_name
=
'
first
'
...
@@ -24,6 +28,12 @@ class TwoScreenApp(App):
...
@@ -24,6 +28,12 @@ class TwoScreenApp(App):
self
.
advancing
=
False
self
.
advancing
=
False
self
.
current_screen_name
=
'
second
'
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
):
def
on_start
(
self
):
self
.
open_first_screen
()
# Once the app starts, we can select the starting screen.
self
.
open_first_screen
()
# Once the app starts, we can select the starting screen.
...
...
This diff is collapsed.
Click to expand it.
second_screen.kv
+
3
−
0
View file @
d1c9173c
...
@@ -5,6 +5,9 @@
...
@@ -5,6 +5,9 @@
text: 'This is the second screen.'
text: 'This is the second screen.'
Label:
Label:
text: root.message # In a custom widget's Kv, `root` refers to the custom widget.
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:
Button:
text: 'Change…'
text: 'Change…'
on_press: app.open_first_screen() # But `app` still refers to the application object.
on_press: app.open_first_screen() # But `app` still refers to the application object.
This diff is collapsed.
Click to expand it.
second_screen.py
+
5
−
1
View file @
d1c9173c
from
kivy.lang
import
Builder
from
kivy.lang
import
Builder
from
kivy.uix.screenmanager
import
Screen
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.
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
...
@@ -8,3 +8,7 @@ Builder.load_file('second_screen.kv') # Because this is not the main app, we mu
class
SecondScreen
(
Screen
):
class
SecondScreen
(
Screen
):
message
=
StringProperty
(
'
Hello, World!
'
)
# This property is changed by twoscreen.kv.
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
}
.
'
)
This diff is collapsed.
Click to expand it.
twoscreen.kv
+
1
−
0
View file @
d1c9173c
...
@@ -11,3 +11,4 @@ ScreenManager:
...
@@ -11,3 +11,4 @@ ScreenManager:
SecondScreen:
SecondScreen:
name: 'second'
name: 'second'
message: 'This is a message!' # The app sends information to the custom widgets by setting their properties.
message: 'This is a message!' # The app sends information to the custom widgets by setting their properties.
roll: app.roll
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment