Hi all,
I want to share solution on this issue. Saw a few posts on this but seems unresolved. I couldn't find anything in the documentation and the forums on how to pass data or variables between SCREENS using the ScreenManager. The properties could not be called by another class neither in python nor in the kv file. Was able to make it work, but I'm not completely sure if I did it right. (PLEASE correct me if I am missing something)
So at first, following the documentation, I made a screenmanager class and screen classes, like so, in skeletal code: (with comments)
***********************************************
*FYI, its an app for breast cancer, so pardon on the names
class Video(Screen):
pass
class CancerMenu(Screen):
variable = StringProperty()
def do_something(self):
self.manager.current='video' #this works, when I call this function (from a button in a kv file) the screen actually changes
# but i cannot pass the info variable to the Video screen. It could not be referenced from the kv file as well
class Cancer(ScreenManager):
def __init__(self, **kwargs):
super(Cancer,self).__init__(**kwargs)
info = StringProperty()
cancer1 = CancerMenu()
video1= Video()
self.add_widget(cancer1)
self.add_widget(video1)
self.current='Welcome'
class CancerApp(App):
def build(self):
return Cancer()
if __name__ == '__main__':
CancerApp().run()
**************************************************************
Kv file was set up like this:
<Video>:
some layout
<CancerMenu>:
some layout
I could not find any way to do this. The properties (in my case, info from the CancerMenu screen cannot be accessed in other screens, either in python nor in the kv file.
Note: I also tried using the same setup as the documentation (
http://kivy.org/docs/api-kivy.uix.screenmanager.html) where the ScreenManager was instantiated in the app class (
sm = ScreenManager() then return sm) umenDidn't work for me, I could not pass variables between screens. Also, for some reason I tried using global variables, but it didn't work, even using the global keyword
The ScreenManager is now set up in the kv file itself under ONE CLASS only to be able to use data between the screens
************************************
python file is now:
class MyScreen(Screen): #used this class just to prevent my kv rules to override the default
pass
class MyScreenManager(ScreenManager):
pass
class Cancer(BoxLayout):
next_screen = StringProperty()
def do_something(self):
self.next_screen = "BSE" #this segment can now be used by all the screens
class CancerApp(App):
def build(self):
return Cancer()
if __name__ == '__main__':
CancerApp().run()
*****************************
KV file is now:
<Cancer>
orientation: 'vertical'
MyScreenManager:
id: screen_manager
current: root.next_screen #this is a property that will always check which screen is to be displayed
MyScreen:
my layouts!
somewhere button press will call a function to change root.next_screen
MyScreen:
my layouts!
Since all the screens are in one class, i can now pass variables. Hope that helps someone!
I still need help on this though, is this an efficient way of using the screenmanager? And how can I go about measuring that in python using linux if anyone can help? Thank you!
Bernard