title: How to use on_press event for a button that has been added dynamically kivy
body: <p>I have the following code where a <code>Button</code> is added to a <code>Grid Layout</code> everytime the <code>test_press</code> method is called. What I'd like to be able to do is change to a different screen when one of these buttons is pressed, but I'd like each <code>Button</code> that's been created to go to a different screen - which screen it goes to isn't important at the moment. How would I go about implementing something like this? </p>
  
  <pre><code>from kivy.uix.scrollview import ScrollView
  from kivy.uix.button import Button
  from kivy.core.window import Window
  from kivymd.theming import ThemeManager
  import kivy
  from kivy.app import App
  from kivy.properties import ObjectProperty, StringProperty, NumericProperty, ListProperty
  from kivy.uix.screenmanager import ScreenManager, Screen
  from kivy.lang import Builder
  from kivy.uix.label import Label
  
  Window.clearcolor = (1,1,1,1)
  
  class Finder(Screen):
      layout_profiles = ObjectProperty(None)
  
      def test_press(self):
          Btn1 = Button(size_hint_y=None, height=75)
          self.layout_profiles.add_widget(Btn1)
  
  class MyApp(App):
      theme_cls = ThemeManager()
  
      def build(self):
          kv = Builder.load_file("kivy.kv")
          sm = WindowManager()
  
          screens = [Finder(name="finder")]
          for screen in screens:
              sm.add_widget(screen)
  
          sm.current = "information"
          return sm
  
  
  if __name__ == '__main__':
      MyApp().run()
  </code></pre>
  
  <p>kv file:</p>
  
  <pre><code>&lt;Finder&gt;:
      name: "finder"
      layout_profiles: layout_profiles
  
      NavigationLayout:
          id: nav_layout
          MDNavigationDrawer:
              NavigationDrawerIconButton:
                  text: "Test"
                  on_release: app.root.current = "login"
  
          FloatLayout:
              MDToolbar:
                  pos_hint: {'top': 1}
                  md_bg_color: 0.2, 0.6, 1, 1
                  left_action_items: [['menu', lambda x: root.ids.nav_layout.toggle_nav_drawer()]]
  
              Label:
                  background_normal: ""
                  background_color: 1, 1, 1, 1
                  color: 0, 0, 0, 1
                  text: "Holding text"
                  font_size: 20
                  multiline: True
                  pos_hint: {"x": 0.125, "y": 0.7}
                  text_size: self.width, None
                  size_hint: 0.75, None
                  height: self.texture_size[1]
                  halign: "center"
  
              ScrollView:
                  size_hint: 0.95, 0.6
                  pos_hint: {"x": 0.025, "top": 0.65, "bottom": 0.2}
                  do_scroll_x: False
                  GridLayout:
                      cols: 1
                      id: layout_profiles
                      size_hint_y: None
                      spacing: 10
                      height: self.minimum_height
                      canvas:
                          Color:
                              rgba: (1, 1, 1, 1)
                          Rectangle:
                              size: self.size
                              pos: self.pos
  
              BoxLayout:
                  cols: 1
                  Button:
                      text: "Add widget"
                      size_hint: 0.5, 0.1
                      pos_hint: {"x": 0.5, "top": 0.1}
                      on_release: root.test_press()
  </code></pre>
  
  <p>Also apologies if the title (or description!) isn't that clear, I wasn't sure how to describe it.</p>
  
  <p>The "Add widget" <code>Button</code> is just there to mimic the eventual goal where the buttons will be added dependent on some information feeding in from a database.</p>