title: Kivy define background color of label
body: <p>I have trouble defining the background color for various objects (in this example labels) in the Kivy language for Python. The attached code creates a box layout, where each box has a certain background color.</p>
  
  <ol>
  <li>How can this code be adapted so that I can define the size of the certain label via a variable, rather than hard code it. Best would be if we could come up with a solution like this:</li>
  </ol>
  
  <pre><code>    ForeignLanguage = ColoredLabel(..., size = (a,b))
  </code></pre>
  
  <ol start="2">
  <li>My code appears quite complicated to me. Is it possible to set the background color ob a label in a more easy way, like Label(..., background_color = (1,1,1,1))?</li>
  </ol>
  
  <pre><code>
      from kivy.app import App
      from kivy.uix.boxlayout import BoxLayout
      from kivy.uix.label import Label
      from kivy.properties import ListProperty
      from kivy.lang import Builder
      from kivy.core.window import Window
  
  
      kv = '''
      &lt;ColoredLabel&gt;:
          size: (self.size_x,self.size_y)
          pos: (0,0) # no effect
          background_color:
          canvas.before:
              Color:
                  rgba: self.background_color
              Rectangle:
                  pos: self.pos
                  size: (self.size_x,self.size_y)
          '''
  
      Builder.load_string(kv)
  
      class ColoredLabel(Label):
          background_color = ListProperty((0,0,0,1))
          s_global = Window.size
          size_x = s_global[0]
          size_y = s_global[1]/3
  
      class MyWidget(BoxLayout):
          #init
          def __init__(self, **kwargs):
              super().__init__(**kwargs)
              ForeignLanguage = ColoredLabel(text="ForeignLanguage", size_hint=(None, None), background_color=(0/255,171/255,169/255, 1))
              Translation = ColoredLabel(text="Translation", size_hint=(None, None), background_color=(45/255,137/255,239/255, 1))
              Example = ColoredLabel(text="Example", size_hint=(None, None), background_color=(43/255,87/255,151/255, 1))
              verticalBox     = BoxLayout(orientation='vertical')
              verticalBox.add_widget(ForeignLanguage)
              verticalBox.add_widget(Translation)
              verticalBox.add_widget(Example)
              self.add_widget(verticalBox)
  
      class BoxLayoutDemo(App):
          def build(self):
              return MyWidget()
  
      if __name__ == '__main__':
          BoxLayoutDemo().run()
  </code></pre>