Trouble with Kivy ScreenManager
<p>Lately I have some problems with Kivy's Screenmanager. I can't use the <code>switch_to</code> method of the manager when accessing it from a <code>Screen</code> class. In a <code>Screen</code> class: <code>self.manager.switch_to</code> gives the error: <code>AttributeError: 'NoneType' object has no attribute 'switch_to'</code>. I'm a bit desperate. Below simple app that produces the error. </p>

<pre><code>import kivy
kivy.require("1.9.1")

from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.screenmanager import ScreenManager, Screen

screen1 = True


class Screen1(Screen):
    def __init__(self, **kw):
        super(Screen1, self).__init__(**kw)
        self.add_widget(Label(text="Screen1"))


class Screen2(Screen):
    def __init__(self, **kw):
        super(Screen2, self).__init__(**kw)
        self.add_widget(Label(text="Screen2"))


class BlackMenu(Screen):
    def __init__(self, **kw):
        super(BlackMenu, self).__init__(**kw)
        if screen1:
            self.manager.switch_to(Screen1())


class MyApp(App):
    def build(self):
        mymanager = ScreenManager()
        mymanager.switch_to(BlackMenu(name="black"))
        return mymanager


if __name__ == "__main__":
    MyApp().run()
</code></pre>

<p>try declaring your screen manager as a global variable below screen1</p>

<pre><code>import kivy
kivy.require("1.9.1")

from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.screenmanager import ScreenManager, Screen

screen1 = True
screen_manager = ScreenManager()
</code></pre>

<p>then in your build function add your screens just like:</p>

<pre><code>def build(self):
    screen_manager.add_widget(Screen1(name='screen1'))
    screen_manager.add_widget(Screen2(name='screen2'))
    screen_manager.add_widget(BlackMenu(name='black_menu'))
    return screen_manager
</code></pre>

<p>then in your black screen you could do:</p>

<pre><code>class BlackMenu(Screen):
def __init__(self, **kw):
    super(BlackMenu, self).__init__(**kw)
    if screen_manager.current == 'screen1':
        screen_manager.switch_to(Screen1())
</code></pre>

<p>but keep in mind that switching to a screen removes that screen from screen_manager childs so you have to manage adding it again.
good luck</p>

python kivy