Search code examples
pythonkivy

KIVY python: why is screen manager none type sometimes


Very often in my code, I need to say in a Screen object this:

self.manager.current = 'screenname'

But sometimes my interpreter says that the None type has no attribute current...

Is it normal that my screen manager disappears?

EDIT:

The problem happens when I add this piece of code to my project:

class EditClass(Screen):
    def __init__(self, **kwargs):
        super(EditClass, self).__init__(**kwargs)
        self.myinit()

    def go_to_home(self):
        self.manager.current = "home_screen"


    def myinit(self):

        self.box0 = BoxLayout(orientation='vertical')
        self.box1 = BoxLayout(spacing=-2, size=(50,50), size_hint=(1,None))
        self.box2 = BoxLayout(orientation='vertical', padding = (5,5,5,5), spacing = 5)

        self.btn_back = Button(size=(32, 50), on_press=self.go_to_home(), size_hint=(None, 1), text="<", background_color=(0.239, 0.815, 0.552, 1))
        self.btn_title = Button(text="Edit a class", background_color = (0.239, 0.815, 0.552, 1))
        self.btn_more= Button(size=(32, 50), size_hint=(None, 1), text="=", background_color = (0.239, 0.815, 0.552, 1))

        self.anchor0 = AnchorLayout(anchor_x='right', anchor_y = 'bottom', padding=(5,5,5,5))
        self.btn_plus = Button(text="+", size=(46, 46), size_hint=(None, None), background_color=(0.239, 0.815, 0.552, 1))

        self.box1.add_widget(self.btn_back)
        self.box1.add_widget(self.btn_title)
        self.box1.add_widget(self.btn_more)

        self.anchor0.add_widget(self.btn_plus)
        self.box2.add_widget(self.anchor0)

        self.box0.add_widget(self.box1)
        self.box0.add_widget(self.box2)
        self.add_widget(self.box0)

Solution

  • Instead of adding myinit to __init__ you could schedule it:

    from kivy.clock import Clock
    
    ...
    class EditClass(Screen):
        def __init__(self, **kwargs):
            super(EditClass, self).__init__(**kwargs)
            Clock.schedule_once(self.myinit, 1)
    
        ...
    
        def myinit(self, *args):
            ...
    
    ...