Change screen by python file in kivy

Asked

Viewed 549 times

1

I need to change the screen inside the file .py, I can’t do it for .Kv because I have to use the ButtonBehavior and it does not work in .Kv.

    from kivy.app import App
    from kivy.uix.boxlayout import BoxLayout
    from kivy.uix.button import ButtonBehavior
    from kivy.uix.image import Image, AsyncImage
    from kivy.uix.screenmanager import ScreenManager, Screen

    import json
    import requests


    class TelaInicio(Screen):
        pass


    class TelaLigas(Screen):
        def on_enter(self, *args):
            url = requests.get('http://127.0.0.1:8000/api/ligas')
            res = json.loads(url.text)
            for i in res:
                self.ids.ligas.add_widget(ListaLigas(ligas=i['imagem']))


    class TelaTorneios(Screen):
        pass


    class ListaLigas(BoxLayout, ButtonBehavior, Image):
        def __init__(self, ligas, manager=None, **kwargs):
            super(ListaLigas, self).__init__(**kwargs)
            self.manager = manager
            self.l = str(ligas)
            self.add_widget(AsyncImage(source=self.l))

        def on_press(self):
            print('ok')
            self.manager.troca_tela_torneios()


    class Manager(ScreenManager):
        def __init__(self, **kwargs):
            super(Manager, self).__init__(**kwargs)
            self.lista_ligas = ListaLigas(self)

        def troca_tela_torneios(self):
            self.current = 'tela_torneios'

        def troca_tela_ligas(self):
            self.current = 'tela_ligas'

        def troca_tela_inicio(self):
            self.current = 'tela_inicio'


    class Main(App):
        def build(self):
            return Manager()


    if __name__ == '__main__':
        Main().run()

The mistake:

File "/home/March/Pycharmprojects/Eanimaapp/main.py", line 36, in on_press self.manager.troca_tela_tournaments() Attributeerror: 'Nonetype' Object has no attribute 'troca_tela_tournaments'

  • When you urge ListaLigas in Manager you pass only one parameter, which will be ligas and therefore, manager will remain None.

  • I understood more or less, now, how can I pass the two parameters? I’m still learning, learned this stop of init yesterday and I was impressed.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.