Open In App

Circular (Oval like) button using canvas 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 about how can we create a rounded or circular button in kivy using canvas. You must have a very clear concept of canvas, button and their properties to learn how can you make buttons like this. As we know canvas is the root object used for drawing by a Widget. Each Widget in Kivy already has a Canvas by default. When you create a widget, you can create all the instructions needed for drawing. 
To use canvas you must have to import the graphics in your file. 
 

from kivy.graphics import Rectangle, Color

 

Some important properties used in this article – 
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)
Button behavior : 
1) The Button Behavior mix in class provides Button behavior. 
2) You can combine this class with other widgets, such as an Image, to provide alternative buttons that preserve Kivy button behavior.
 

 

Basic Approach -
-> import kivy
-> import kivy App
-> import widget
-> import Canvas i.e.:
      from kivy.graphics import Rectangle, Color
-> set minimum version(optional)
-> Extend the Widget class
-> Create the App Class
-> create the .kv file:
    -> create the button using the canvas
    -> Use border property to give them a circular shape.
    -> Add action/callback if needed
-> return a Widget
-> Run an instance of the class

 

Kivy Tutorial – Learn Kivy with Examples

Implementation of the Approach
main.py 
 

Python3




## Sample Python application demonstrating the
## working of canvas with buttonbehaviour i.e
## creating a circular button 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
 
# From graphics module we are importing
# Rectangle and Color as they are
# basic building of canvas.
from kivy.graphics import Rectangle, Color
 
# The ButtonBehavior mixin class provides Button behavior.
from kivy.uix.button import ButtonBehavior
 
# The Label widget is for rendering text.
from kivy.uix.label import Label
 
# class in which we are creating the canvas
class CircularButton(ButtonBehavior, Label):
    pass
 
# Create the App Class
class BtnApp(App):
    def build(self):
        return CircularButton()
 
# run the App
BtnApp().run()


.kv file 
 

Python3




# .kv file of creating a circular button using canvas
 
<CircularButton>:
 
    # Creating Circular button
    canvas:
 
        # Color is different if button is pressed
        Color:
            rgb: (0, 1, 0, 1) if self.state == 'normal' else (1, 0, 1, 1)
     
        # Rounded rectangle canvas
        RoundedRectangle:
 
                        # Giving the size and the position
            size: (self.size)
            pos: (self.pos)
             
            # This will force the rectangle to be the circle
            radius: [400, ]
         
 
    # Print the text when touched or button pressed    
    on_release:
        print("This is the button made up by the canvas")


Output: 
 

 

Note: Widgets are still rectangles. That means that even if you click on the rounded corners, the button still receive the event.
 



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