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
import kivy
kivy.require( "1.9.1" )
from kivy.app import App
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.button import Button
kivy.require( '1.9.0' )
from kivy.config import Config
Config. set ( 'graphics' , 'resizable' , True )
class Base(FloatLayout):
pass
class BtnApp(App):
def build( self ):
return Base()
if __name__ = = "__main__" :
BtnApp().run()
|
btn.kv
Python3
<Base>:
Button:
text: 'Hit me !!'
background_normal: 'normal.png'
background_down: 'down.png'
border: 30 , 30 , 30 , 30
background_color: 0.1 , 0.5 , 0.6 , 1
size_hint: . 3 , . 3
pos_hint: { "x" : 0.35 , "y" : 0.3 }
|
Output:

Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
19 Oct, 2021
Like Article
Save Article