How to transfer a list of element from a screen to another using Kivy

895 Views Asked by At

I have some trouble to transfert a list of element, that i get from a file (in the first screen), to the second and third screen.

I have 3 Screen managed by ScreenManager, every screen is in a different file.py and have a file.kv. This is what my ScreenManager looks like (filename : login.py):

from interface1 import Interface1App
from interface2 import Interface2App

class LoginApp(App):
    def build(self):
        manager = ScreenManager()

        #add view login
        manager.add_widget(Login(name='login'))

        # add view 'interface1'
        app = Interface1App()
        app.load_kv()
        interfacen1 = app.build()
        manager.add_widget(interfacen1)

        # add view 'interface2'
        app2 = Interface2App()
        app2.load_kv()
        interfacen2 = app2.build()
        manager.add_widget(interfacen2)

        # add view 'interface3'
        app3 = Interface3App()
        app3.load_kv()
        interfacen3 = app3.build()
        manager.add_widget(interfacen3)

        manager.transition = SlideTransition(direction="left")
        return manager

In the screen manager login i import all the screens (ex: from interface1 import Interface1App)

In the second file, i put in self.list some data, and use it for that interface (filename : interface1.py):

from interface2 import Interface2App
class AllImage(Screen):
    CONTAINER_PNG = os.path.join(AllImage_ROOT, 'images')
    IMAGES_NAMES = [c[:-4] for c in os.listdir(CONTAINER_PNG)]

    def __init__(self, **kwargs):
        Screen.__init__(self, **kwargs)
        for im in IMAGES_NAMES:
            if IMAGES_NAMES != None :


                toggleimage = ToggleWithImage(src=im+'.png')
                toggleimage.bind(on_press=  lambda a, im=im:self.onpress_addpage(self.listim, im)) 
                self.layout.add_widget(toggleimage)

    def change_screen(self, x, list): 

        self.manager.transition = SlideTransition(direction="left")
        self.manager.current = 'interface2'
        self.manager.get_screen('interface2')
        self.manager.list1 = [1,2,3]


    def backtologin(self):
        self.manager.transition = SlideTransition(direction="right")
        self.manager.current = 'login'
        self.manager.get_screen('login')


class Interface1App(App):
    '''The kivy App that runs the main root. All we do is build a AllImage
    widget into the root.'''

    def build(self):

        screen = AllImage()
        screen.name = 'interface1'
        return screen

After that i want to use again the data collected in the 2nd interface (self.list) and use it in the 3rd Interface (filename: interace2.py):

from interface1 import AllImage #this isn't working
class Combinaison(Screen):


    screen = ObjectProperty(None)    

    def __init__(self, **kwargs):
        # self._previously_parsed_text = ''
        super(Combinaison, self).__init__(**kwargs)
        # self.list = list(self.manager.get_screen('interface1').list)
        print(self.manager.list1) # The problem is here <===========


    def backtointerface1(self):
        self.manager.transition = SlideTransition(direction="right")
        self.manager.current = 'interface1'
        self.manager.get_screen('interface1')

class Interface2App(App):
    '''The kivy App that runs the main root. All we do is build a Combinaison
    widget into the root.'''

    def build(self):
        screen = Combinaison()
        screen.name = 'interface2'
        return screen

With this code i have this error : print(self.manager.list1) AttributeError: 'NoneType' object has no attribute 'list1'

I think i didn't understand how i should use the self.manager.list1=[1, 2, 3]

I hope that i was clear, sorry for the long post. Thank you for your time :)

1

There are 1 best solutions below

17
On BEST ANSWER

You can use Screen.manager.get_screen(name).list = list(self.list)

A complete example:

from kivy.app import App
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.uix.button import Button


class Screen1(Screen):

    list = [1,2,3]

    def __init__(self,**kwargs):

        super(Screen1,self).__init__(**kwargs)
        self.switch_button = Button(text="goto 2")
        self.switch_button.bind(on_release=self.switch)
        self.add_widget(self.switch_button)

    def switch(self,*args):
        self.list = [4,5,6]
        self.manager.get_screen("screen2").list = list(self.list)
        self.manager.current = "screen2"


class Screen2(Screen):

    def __init__(self,**kwargs):

        super(Screen2,self).__init__(**kwargs)
        self.switch_button = Button(text="goto 1")
        self.switch_button.bind(on_release=self.switch)
        self.add_widget(self.switch_button)

    def switch(self,*args):
        self.manager.current = "screen1"

    def on_enter(self):
        print self.list


class MainApp(App):

    def build(self):

        sc1 = Screen1(name="screen1")
        sc2 = Screen2(name="screen2")

        self.sm = ScreenManager()

        self.sm.add_widget(sc1)
        self.sm.add_widget(sc2)

        return self.sm


MainApp().run()

You can also use the ScreenManager to store the list or lists.

self.manager.list1 = [1,2,3] # dont call this in __init__
self.manager.list2 = [4,5,6] # call it in on_enter instead

Those lists will be available from all screens after they are added to the manager. The on_enter method on the screen would be suitable for your example.