Circular (Oval like) button using canvas 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 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 ButtonBehavior mixin 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 needeed
-> return a Widget
-> Run an instance of the class

Kivy Tutorial – Learn Kivy with Examples

Implementation of the Approach

main.py

filter_none

edit
close

play_arrow

link
brightness_4
code

## 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() 

chevron_right


.kv file

filter_none

edit
close

play_arrow

link
brightness_4
code

# .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"

chevron_right


Output:

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




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.