How to share variable across modules in Python or Kivy
Previously, I answered several questions on Stackoverflow.com about how to share values among different Kivy. In fact, you can handle it in a python way without using kivy framework at all. Here I will conclude both Kivy way and python way.
1. Global variable
This is an approach using the built-in python keyword - global
. If the 2 screens are in the same file, use the global
variable so they can both access. That means, when you want to use the value, no matter for the first time or for later using, you need to use the keyword global
to let the compiler know that it’s not a local variable.
Disadvantage of this approach is quite obvious, that means if you use it too much, the global
variables will be everywhere in your codebase, and it’s hard to trace its value change or later refactoring.
1 | def create_new_number(): |
2. Use a separate module to hold all the shared variables.
It’s simple, use a single module to hold all the variables you want to use. And next, whenever you want to use them, just import this module, and use it. Don’t worry, it will always use the latest value no matter how you set it in the original separate module.
2.1 A python example looks like this:
1 | # Separate file to hold the shared variables |
The first file who need this variable
1 | # first.py |
Now read the final result from a third file
1 | # main.py |
2.2 Kivy example
Here is the file who holds the variable need to share:
1 | # GlobalShared.py |
Here is the main file to hold the 2 screen, since now we use GlobalShared.py
to hold the variable, it doesn’t matter whether the 2 screens are in the same file or in 2 different files, here I put them all in main.py
just for a quick hack. You can separate them if you need.
Every screen has 3 buttons
- Read: read value of MY_NUMBER
- Change: add MY_NUMBER by 1
- Go: Go to next screen
You can see how it read the shared variable and change it while the other screen could still access and change the variable.
1 | # main.py |
3. Kivy way
The easiest way is Kivy way:
- declare a class-level variable at your
App
class - Then you can get the value by
app.get_running_app().your_class_variable_name
- Don’t forget
from kivy.app import app
That means you can use the kivy way to create a middle variable to manage the communication between two modules. this one is quick since you don’t need a new file.
Below is the main file to hold the 2 screens, it doesn’t matter whether the 2 screens are in the same file or in 2 different files, here I put them all in main.py
just for a quick hack. You can separate them if you need.
Every screen has 3 buttons
- Read: read value of MY_NUMBER
- Change: add MY_NUMBER by 1
- Go: Go to next screen
Notice: You don’t need a separate file now since all your shared variables will stored at your app class like the
MY_NUMBER
inHandSetApp
below.
You can see how the two screens can access the same shared variable and change it
And I show 2 way here to access the shared variable:
- how to access it in
.kv
file.- use the
app.XXXX
to access your app instance from.kv
- use the
- how to access it in
.py
file.- use
App.get_running_app().XXXX
to access the variable
- use
1 | from kivy.app import App |
4. End
I love the separate module since it’s easy and clean. Kivy way is sure quick, but if your variables are too many, your app class will become pretty ugly.
Thanks for reading!
Follow me (albertgao) on twitter, if you want to hear more about my interesting ideas.