Python – Change button color in kivy using .kv file

Kivy is a platform independent GUI tool in Python. As it can be run on Android, IOS, Linux and Windows, etc. It is basically used to develop the Android application, but it does not mean that it can not be used on Desktops applications.

In this article we will learn how to change the background color of button in kivy in .kv file.

background_color: There is a property name background color which is used to change the the color of the button in kivy python. The background-color kivy property sets the background color of an element. The background-color property is specified as a single color value.

Syntax: background_color: 1, 0, 0, 1

Note: By default the color of button is black and it only takes the value between 0 to 1



Basic Approach:

1) import kivy
2) import kivyApp
3) import Widget
4) import Button
5) Set minimum version(optional)
6) Create widget class
7) create App class
8) create .kv file (name same as the app class):
        1) Create Widget
        2) Create Button
        3) set the background color of the button as you want   
        4) Specify requirements
9) return Layout/widget/Class(according to requirement)
10) Run an instance of the class

Kivy Tutorial – Learn Kivy with Examples.

Code to implement the above Approach to color the button using kv file.

.py file

filter_none

edit
close

play_arrow

link
brightness_4
code

## Sample Python application demonstrating the 
## How to change button color in Kivy using .kv file 
  
###################################################
# import modules 
import kivy 
  
# base Class of your App inherits from the App class. 
# app:always refers to the instance of your application 
from kivy.app import App 
  
# This layout allows you to set relative coordinates for children. 
from kivy.uix.relativelayout import RelativeLayout 
  
# To change the kivy default settings 
# we use this module config 
from kivy.config import Config 
  
# creating the root widget used in .kv file 
class RelativeLayout(RelativeLayout): 
    pass
  
# creating the App class in which name 
#.kv file is to be named Btn.kv 
class BtnApp(App): 
    # defining build() 
    def build(self): 
        # returning the instance of root class 
        return RelativeLayout() 
  
# run the app 
if __name__ == "__main__"
    BtnApp().run() 

chevron_right


Btn.kv file implementation of main.py file

filter_none

edit
close

play_arrow

link
brightness_4
code

#.kv file implementation of color btn 
  
<RelativeLayout>: 
  
    Button: 
          
        text:"Colorful"
        # Change the default color to your choice
        background_color: 0.1, 0.5, 0.6, 1
        pos_hint: {"x":0.2, "y":.4}
        size_hint: 0.3, 0.2
  
  
    Button: 
        text:"Default"
  
        # The default color of a button
        background_color: 1, 1, 1, 1
        pos_hint: {"x":.6, "y":.4}
        size_hint: 0.3, 0.2
  
                     

chevron_right


Output:

Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.




My Personal Notes arrow_drop_up

Competative Programmer

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.