Mike Delta
Mike Delta

Reputation: 808

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)

Upvotes: 0

Views: 353

Answers (3)

Daniel Exxxe
Daniel Exxxe

Reputation: 69

As John Anderson mentioned earlier, the ScreenManager didn't add the Screen yet.

Working Example:

from kivy.app import App
from kivy.uix.screenmanager import ScreenManager, Screen
class test_app(App):
    def build(self):
        SM = ScreenManager()
        sc = Screen(name='your_screen_name')
        print(sc.manager) #None (*1)
        SM.add_widget(sc)
        print(sc.manager) #SM (*2)
        return SM
test_app().run()

Note, (1) You cannot access the self.manager attribute until you finish your __init__ method. (2) You have access to your manager now.

Upvotes: 0

sp________
sp________

Reputation: 2645

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):
        ...

...

Upvotes: 1

John Anderson
John Anderson

Reputation: 38837

self.manager is None in a Screen object until that object has been added to a ScreenManager with the add_widget() method.

Upvotes: 1

Related Questions