title: How to create labels using for loop in Kivy's .kv file?
body: <p>I am working on a PDF parsing app in Kivy. I am using screen managers in .py file. In one of the screens, I select the pdf file and add it to a list, update a function in another screen ('Files') and then switch to that screen. My .py file is as under:- </p>
  
  <pre><code>import kivy
  from kivy.app import App
  from kivy.uix.label import Label
  from kivy.uix.gridlayout import GridLayout
  from kivy.uix.textinput import TextInput
  from kivy.uix.boxlayout import BoxLayout
  from kivy.uix.popup import Popup
  from kivy.uix.button import Button
  from kivy.uix.screenmanager import ScreenManager, Screen
  from kivy.lang import Builder
  kivy.require("1.11.1")
  
  from kivy.uix.floatlayout import FloatLayout
  from kivy.config import Config
  
  
  from kivy.properties import ObjectProperty
  from kivy.uix.widget import Widget
  Config.set('graphics', 'resizable', True)
  
  from PyPDF2 import PdfFileReader, PdfFileWriter
  
  FILE_LIST = []
  PAGE_LIST = []
  OUTPUT_LIST = []
  
  
  def add_output_list(page_name):
      for item in PAGE_LIST:
          if item[0] == page_name:
              if item[-1] not in OUTPUT_LIST:
                  OUTPUT_LIST.append(item[-1])
      print(OUTPUT_LIST)
  
  
  class FinalPage(GridLayout):
      def __init__(self, **kwargs):
          super().__init__(**kwargs)
          self.cols = 1
  
  
  class ParsingPage(GridLayout):
      def __init__(self, **kwargs):
          super().__init__(**kwargs)
          self.cols = 4
  
      def update_pages(self):
          for item in PAGE_LIST:
              self.inside = GridLayout()
              self.inside.cols = 3
  
              self.inside.select = Button(text="sel")
              self.inside.add_widget(self.inside.select)
              self.inside.select.bind(on_press=lambda x: self.select_button(item[-1]))
  
              self.inside.clock = Button(text="c")
              self.inside.add_widget(self.inside.clock)
  
              self.inside.anticlock = Button(text="ac")
              self.inside.add_widget(self.inside.anticlock)
  
              self.add_widget(self.inside)
              self.add_widget(Label(text=f'{item[0]}'))
          self.inside_2 = GridLayout()
          self.inside_2.cols = 2
          self.inside_2.done = Button(text="Done")
          self.inside_2.add_widget(self.inside_2.done)
          self.inside_2.done.bind(on_press=self.done_button)
  
          self.inside_2.cancel = Button(text="Cancel")
          self.inside_2.add_widget(self.inside_2.cancel)
          self.inside_2.cancel.bind(on_press=self.cancel_button)
  
      def select_button(self, page_name):
          add_output_list(page_name)
  
      def done_button(self):
          pass
  
      def cancel_button(self):
          pass
  
  
  class SelectionPage(Widget):
  
      def select(self, *args):
          try:
              if args[1][0].split('.')[-1] != 'pdf':
                  self.label.text = 'You can only select a PDF File.'
              else:
                  self.label.text = args[1][0]
          except:
              pass
  
      def add_button(self):
          FILE_LIST.append(self.label.text)
          pdf_app.files_page.update_files(self.label.text)
          pdf_app.screen_manager.current = 'Files'
  
      def next_button(self):
          pdf_app.screen_manager.current = 'Files'
  
  
  class FilesPage(Widget):
      def update_files(self):
          return FILE_LIST
  
  
  class BrowsePage(Widget):
  
      def browse_button(self):
          pdf_app.screen_manager.current = 'Selection'
  
  
  class PdfParserApp(App):
      FILE_LIST = []
  
      def build(self):
  
          self.screen_manager = ScreenManager()
          self.browse_page = BrowsePage()
          screen = Screen(name='Browse')
          screen.add_widget(self.browse_page)
          self.screen_manager.add_widget(screen)
  
          # Info page
          self.selection_page = SelectionPage()
          screen = Screen(name='Selection')
          screen.add_widget(self.selection_page)
          self.screen_manager.add_widget(screen)
  
          self.files_page = FilesPage()
          screen = Screen(name='Files')
          screen.add_widget(self.files_page)
          self.screen_manager.add_widget(screen)
  
          self.parsing_page = ParsingPage()
          screen = Screen(name='Parsing')
          screen.add_widget(self.parsing_page)
          self.screen_manager.add_widget(screen)
  
          return self.screen_manager
  
  
  if __name__ == '__main__':
      pdf_app = PdfParserApp()
      pdf_app.run()
  
  </code></pre>
  
  <p>FILE_LIST is a variable outside all the classes (so that it can be used among the classes.</p>
  
  <p>Now, I want to loop through this FILE_LIST and create labels on the next screen. But I want to do it using .kv files, so that I can keep uniformity in design across the application. My .kv file is :-</p>
  
  <pre><code>#, kv file implementation
  #:import Label kivy.uix.label.Label
  
  &lt;BrowsePage&gt;:
      GridLayout:
          size : root.width-200, root.height-200
          pos : 100, 100
          cols : 1
          Label :
              text: "Welcome to PDF Parser"
              color: [ 66/255, 103/255, 178/255, 1]
              font_size: 38
              size_hint : (0.2, 0.5)
          Label :
              text : "Select the file(s)."
              color: [ 66/255, 103/255, 178/255, 1]
              font_size: 20
              size_hint : (0.2, 0.5)
          AnchorLayout:
              anchor_x : "center"
              Button:
                  text : "Browse"
                  size_hint : (.15, .15)
  
                  on_press : root.browse_button()
  
  &lt;SelectionPage&gt;:
  
      label: label
  
  
      GridLayout:
          size : root.width, root.height
          cols :1
  
          FileChooserIconView:
  
              pos_hint: {"x":0, "top" : 1}
              on_selection: root.select(*args)
  
          Label:
  
              id: label
              size_hint : (.1, .1)
  
          GridLayout:
              cols : 3
              size_hint : (1, .15)
  
              AnchorLayout:
                  anchor_x : "center"
                  Button:
                      text : "Cancel"
                      size_hint : (.15, .15)
  
              AnchorLayout:
                  anchor_x : "center"
                  Button:
                      text : "Add"
                      size_hint : (.15, .15)
                      on_press: root.add_button()
              AnchorLayout:
                  anchor_x : "center"
                  Button:
                      text : "Next"
                      size_hint : (.15, .15)
                      on_press: root.next_button()
  
  
  
  &lt;FilesPage&gt;
      GridLayout:
          size : root.width, root.height
  
          cols: 1
          on_parent:
              for i in root.update_files(): txt = "Label {0}".format(i); self.add_widget(Label(text = txt, text_size=(cm(2), cm(2)), pos=self.pos,
              id=txt, color=(1,1,1,1)))
  
  
  </code></pre>
  
  <p>I assume that when I switch the screen, it somehow create a fresh empty list. The looping code doesn't have any error, as if I hard code a list in my FilePage, then the labels appear.</p>
  
  <p>Any help will be appreciated.</p>