Search code examples
pythonkivykivy-language

How do I change the background color of a button in Kivy?


I am attempting to build a simple app that plays music every time a click on a button that I built in kivy. I would like to add graphics and styling, as the default color is just plain grey. How do I do I change that color to red? I have tried implementing the background_color parameter but that does not change anything. I have attached my code below:

   <Test>:
      do_default_tab: False
      Widget:
         canvas.before:
            Rectangle:
                size: self.size
                pos: self.pos


     TabbedPanelItem:

         text: 'Opera'

         text_size: self.size


         BoxLayout:
            orientation: 'vertical'
            padding: 20
            spacing: 10

            Button:

               text: 'Nessun Dorma'
               text_size: self.size
               on_press: root.nessun_dorma()


               halign: 'center'
               valign: 'middle'

               font_size: 20

Solution

  • You can use state and background_color to achieve the wanted behavior

    <FunkyButton>:
         background_color: (1,0,0,1) if self.state == 'normal' else (0,1,0,1)
         background_normal: ""
         #background_down: "" #optional if you want your color pressed 
    

    color unchanged...

    this way the button will be red when is not pressed and green when it is...