Table of contents
1.
Introduction
2.
Buttons
3.
Creation of Button With Color
3.1.
Button Creation with color Using .kv File
3.1.1.
Python Code
3.2.
Implementation of Btn.kv file of the main.py file
3.2.1.
Program
3.2.2.
 Output
4.
Frequently Asked Questions
4.1.
How to set the background colour of the button in kivy?
4.2.
What is the box layout in kivy?
4.3.
What is a widget in kivy?
4.4.
What is float layout in kivy?
5.
Conclusion
Last Updated: Mar 27, 2024
Medium

Changing Button Color in Kivy Using .kv File

Author Anjali
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

In this article, we will be discussing a crucial concept related to Buttons. We will discuss how to create a button with colour using the .kv file in kivy and how to change their colour by using the kv file, just like the buttons we use in calculators and many more places. 

Kivy is a GUI tool in python which is platform-independent. It can run on any Android, IOS, Linux, Windows, etc. It is mainly used to develop the Android application, but it does not mean that it can not be used on Desktop applications. There are a lot more interesting features and tools available in kivy that you can learn about. To help you in that pursuit, check out a few of our articles like  Float Layout in KivyCarousel Widget in KivyButton Action in Kivy, and Slider Widget in Kivy

Buttons

A button is a Label with some associated actions, and those actions are triggered on the button press (or released after a click or touch). We can also add functions behind the button and style the button.  Here we are going to use an implementation of two buttons, one with a custom colour and the other with the default color. For this, we will use a .kv file. We have also covered the implementation of Button Color in Kivy without using a kv file, so be sure to check that out.

Creation of Button With Color

The basic approach followed while creating buttons is 

  • Importing kivy
  • Importing the kivy App
  • Importing the widget
  • Importing the Button
  • set the minimum version(it is optional)
  • Create Widget Class
  • Create App Class
  • Create .kv File ( Same name as the App class)
    • Create Widget
    • Create Button
    • Set the background colour of the button as per your choice
    • Specify Requirements
  • Return the layout/class/widget (as per requirement)
  • Run an instance of the class

Button Creation with color Using .kv File

As we have seen above the steps are to be followed while creating a button with color using the .kv file. Now we will be discussing with the help of a program. 

The python code will set up our class, but we will define our buttons and their subsequent customisations using a separate .kv file. 

Python Code

## Sample Python code demonstrating
## How to change the button color using the .kv file in Kivy


###################################################
# import kivy module
import kivy


# base Class of the App inherits from App class.
# app: it always refers to the instance of your application
from kivy.app import App


# This layout allows to set the relative coordinates for children.
from kivy.uix.relativelayout import RelativeLayout


# To change the kivy default settings
# we used this module config
from kivy.config import Config


# creating root widget used in the .kv file
class RelativeLayout(RelativeLayout):
    pass


# App class creation in which name.kv file
# is to be named Btn.kv
class SampleBtnApp(App):
    # defining build()
    def build(self):
        # returning the instance of root class
        return RelativeLayout()


# run the app
if __name__ == "__main__":
    SampleBtnApp().run()
You can also try this code with Online Python Compiler
Run Code

Implementation of Btn.kv file of the main.py file

Program

#.kv file implementation of color btn


<RelativeLayout>:


    Button:
        
        text:"Ninja"
        # Change the default color to your choice
        background_color: 255,165,0,0.8
        pos_hint: {"x":0.2, "y":.4}
        size_hint: 0.3, 0.2



    Button:
        text:"Coder"


        # The default color of a button
        background_color: 1, 1, 1, 1
        pos_hint: {"x":.6, "y":.4}
        size_hint: 0.3, 0.2

 Output

In this output, the button will not cover the whole window as we have given a specific size of the button, so by default, it will not show button size equal to window size.

button of specific size in kivy

By default the colour of the “Default Button” is greyish and if you want to change it then you have to use this property, and it only takes the value in the range 0 to 1. If any other value is given, it will lead to misbehaving in the program.

Frequently Asked Questions

How to set the background colour of the button in kivy?

The background-color kivy is the property which sets the background color of an element. 

Syntax:

background_color: 1, 0, 0, 1

What is the box layout in kivy?

The BoxLayout arranges the children in a vertical or horizontal box. For example: layout = BoxLayout(orientation='vertical').

What is a widget in kivy?

A Widget is a base building block of the GUI interfaces in Kivy. It provides a Canvas that can be used to draw on the screen.

What is float layout in kivy?

The FloatLayout honors pos_hint and size_hint properties of its children. For example: layout = FloatLayout(size=(300, 300)).

Conclusion

In this blog, we started with the kivy framework and button introduction. Then we discussed the creation of a button by adding colour to it using .kv file in kivy.  After that, we discussed how to change the button colour and its background colour with the help of a simple program.

 

We hope that this blog helped you enhance your knowledge to improve your understanding of changing the button colours in kivy using the .kv file.
You may also want to learn more so please visit interesting articles about buttonskivy, and python libraries.
Learning never stops, and to learn more and become more skilled, head over to our practice platform Coding Ninjas Studio, practice top problems, attempt Mock Tests, read informative blogs, and interview experiences. Do upvote our blog to help other ninjas grow. 

 

Happy Learning!

Live masterclass