Using ScreenManager for multiple screens and passing user data

3,741 views
Skip to first unread message

happ...@gmail.com

unread,
Feb 20, 2013, 1:03:16 PM2/20/13
to kivy-...@googlegroups.com
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.info='Info I want'
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()
        cancer1.name='Welcome'
        video1.name='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


I was able to make it work using a similar setup to this: https://github.com/kivy/kivy/issues/634
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





Gabriel Pettier

unread,
Feb 20, 2013, 5:31:02 PM2/20/13
to kivy-...@googlegroups.com
On Wed, Feb 20, 2013 at 10:03:16AM -0800, happ...@gmail.com wrote:
> 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)

I think they didn't get answer because they conveyed a misunderstanding,
sharing info between screen is sharing info in your app which is usually
done using properties, there is nothing specific to screenmanager in
that, imho. I'll do a small example of a few ways one can share info
between screens, using either kv or full python.

here it is
https://gist.github.com/5000276

>
> 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

Great to see kivy is involved in saving lives :)

hopes this helps :)
> --
> You received this message because you are subscribed to the Google Groups "Kivy users support" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to kivy-users+...@googlegroups.com.
> For more options, visit https://groups.google.com/groups/opt_out.
>
>
Message has been deleted

happ...@gmail.com

unread,
Mar 1, 2013, 7:00:26 AM3/1/13
to kivy-...@googlegroups.com
Ok. thanks so much! that pretty much is how I did it. Will share the app once its all done :)
Reply all
Reply to author
Forward
0 new messages