title: Setting Global variables Kivy
body: <p>I am using the Screen Manager to manage several different screens. One screen has two buttons that both lead to another screen, but depending on which button was pressed, I would like a label to display different text on the second screen. Here is my code:</p>
  
  <p>.py</p>
  
  <pre><code>MY_GLOBAL = "test"
  
  class ChooseProgScreen(Screen):
      global MY_GLOBAL
      def setTitle(self, newTitle):
          MY_GLOBAL = newTitle
          print(MY_GLOBAL)
  
  class SwitchScreen(Screen):
      global MY_GLOBAL
      def getTitle(self):
          return MY_GLOBAL
  
  class ScreenManagement(ScreenManager):
      pass
  
  class MainApp(App):
      def build(self):
          presentation = Builder.load_file("kivy.kv")
          return presentation
  </code></pre>
  
  <p>.kv</p>
  
  <pre><code>ScreenManagement:
      transition: FadeTransition()
      HomeScreen:
      ChooseProgScreen:
      SwitchScreen:
      NewProgScreen:
  
  &lt;ChooseProgScreen&gt;:
      name: "chooseprog"
      FloatLayout:
          Button:
              text: "test1"
              on_release: 
                  root.setTitle("test1")
                  app.root.current = "switch"
              color: 1,1,1,1
              font_size: 25
              size_hint: 0.15,0.15
              pos_hint: {"center_x":.1, "center_y":.9}
          Button:
              text: "test2"
              on_release:
                  root.setTitle("test2")
                  app.root.current = "switch"
              color: 1,1,1,1
              font_size: 25
              size_hint: 0.15,0.15
              pos_hint: {"center_x":.3, "center_y":.9}
  
  &lt;SwitchScreen&gt;:
      name: "switch"
      FloatLayout:
          Label:
              text: root.getTitle()
              pos_hint: {"center_x":.1, "center_y":.1}
              font_size: 25
  </code></pre>
  
  <p>In ChooseProgScreen in .kv, when the button is released, I call a method from the .py file that sets the global variable to a new screen and prints it. When you press one of the buttons, the print part works fine, and the global variable prints as the new string, but the SwitchScreen label still shows </p>
  
  <pre><code>"test"
  </code></pre>
  
  <p>and not </p>
  
  <pre><code>"test1" or "test2"
  </code></pre>
  
  <p>I think global variables is probably a terrible way of doing this, but I am at a loss for how else to do it using the Screen manager and the kivy language. If someone could help use global variables properly, or suggest a better way to do this, that would be greatly appreciated. </p>
  
  <p><strong>EDIT</strong></p>
  
  <p>Problem was that the screen only updated upon the first load. I added an update method to SwitchScreen:</p>
  
  <pre><code>def update(self):
      self.ids.switchtitle.text = self.getTitle()
  </code></pre>
  
  <p>and updated the SwitchScreen in the .kv file:</p>
  
  <pre><code>&lt;SwitchScreen&gt;:
      on_enter:
          root.update()
      Label:
          id: switchtitle
          text: root.getTitle()
          pos_hint: {"center_x":.1, "center_y":.1}
          font_size: 25
  </code></pre>