Open In App

Python – Rounding button corners in kivy using .kv file

Improve
Improve
Like Article
Like
Save
Share
Report

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 going to learn how to round the button corners in kivy using .kv file in Python.
Widgets are always rectangle, but we can change the background of widgets and put couple of images for the normal and the down state of the button by using some properties of buttons like background_normal and background_down properties respectively. Also, to round the corners of the button you also must have understand one more property of button that is the border property.
 

background_down : 
1) Background image of the button used for the default graphical representation when the button is pressed. 
2) background_down is a StringProperty .
background_normal : 
1) Background image of the button used for the default graphical representation when the button is not pressed. 
2) background_normal is also a StringProperty
border : 
1) Border used for BorderImage graphics instruction. Used with background_normal and background_down. Can be used for custom backgrounds. 
2) It must be a list of four values: (bottom, right, top, left). 
3) border is a ListProperty and defaults to (16, 16, 16, 16) 
 

Syntax of all above properties: 
 

Python3




background_normal: 'normal.png'
background_down: 'down.png'
border: 30, 30, 30, 30


Basic Approach:

1) import kivy
2) import kivyApp
3) import button and floatlayout
4) set minimum version(optional)
5) Create the Layout class
6) Create App class
7) Create .kv file:
          1) Add Base class
          2) Add Button properties
          3) Add Image as button
          4) Round the corners of the button using border property
8) return instance of the layout class
9) Run an instance of the class

Kivy Tutorial – Learn Kivy with Examples.

Below is the two images that is used by me in this i.e:
normal.png and down.png 
 

 

Now below is the code implementing my approach:
main.py 
 

Python3




## Sample Python application demonstrating that  
## how to create button corners round in kivy using .kv file
      
##################################################         
# import kivy module
import kivy
   
# this restrict the kivy version i.e
# below this kivy version you cannot
# use the app or software
kivy.require("1.9.1")
   
# base Class of your App inherits from the App class.
# app:always refers to the instance of your application
from kivy.app import App
 
# module consist the floatlayout
# to work with FloatLayout first
# you have to import it
from kivy.uix.floatlayout import FloatLayout
 
# creates the button in kivy
# if not imported shows the error
from kivy.uix.button import Button
 
# this restrict the kivy version i.e  
# below this kivy version you cannot  
# use the app or software  
kivy.require('1.9.0'
    
# to change the kivy default settings we use this module config
from kivy.config import Config
    
# 0 being off 1 being on as in true / false
# you can use 0 or 1 && True or False
Config.set('graphics', 'resizable', True)
 
# creating the root widget used in .kv file
class Base(FloatLayout):
    pass
 
# class in which we are creating the imagebutton
# in .kv file to be named Btn.kv
class BtnApp(App):
    # defining build()
    def build(self):
        # returning the instance of root class
        return Base()
 
# run function runs the whole program
# i.e run() method which calls the target
# function passed to the constructor.
if __name__ == "__main__":
    BtnApp().run()


btn.kv
 

Python3




#.kv file implementation of rounding the corners of button 
 
 
# create a fully styled functional button
# Adding images normal.png and down.png
<Base>:
    Button:
        text: 'Hit me !!'
 
        # Button properties for image
        background_normal: 'normal.png'
        background_down: 'down.png'
 
        # To round the corners we use border
        border: 30, 30, 30, 30
 
        # Adding background color to button you can comment it
        background_color: 0.1, 0.5, 0.6, 1
         
        size_hint: .3, .3
        pos_hint: {"x":0.35, "y":0.3}


Output: 
 

 



Last Updated : 19 Oct, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads