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 –
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
Implementation of the Approach
Note: Widgets are still rectangles. That means that even if you click on the rounded corners, the button still receive the event.
- Python | Canvas in Kivy using .kv file
- Disable kivy button using .kv file
- Add image button using .kv file in kivy
- Python | Toggle button in kivy using .kv file
- Python - Rounding button corners in kivy using .kv file
- Text Input box with a verification button in kivy (using .kv file)
- Python - Change button color in kivy using .kv file
- Python - Change kivy button size and position using kv file
- Python | Canvas in kivy
- Python | Line (Canvas) in kivy
- Use image as a button in kivy
- Disable Kivy Button
- Python | Button Action in Kivy
- Change button Color in Kivy
- Text Input box with a verification button in kivy
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.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.