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 python.
Now one question arises – What is the preferred way to create rounded corners for buttons in kivy?
Basically this is the tricky one. As we know 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_down properties respectively.
Also to round the corners of the button you also must have understand one more property of button that is the
1) Background image of the button used for the default graphical representation when the button is pressed.
2) background_down is a StringProperty .
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
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:
Basic Approach: -> import kivy -> import kivy App -> import button -> set minimum version(optional) -> Extend the class : -> create an image a button -> Do styling -> Use the border property to round the corners of the button -> Arrange call back if needed -> Add and return a button -> Run an instance of the class
With this two images called
normal.png and down.png, you can start adding your round borders.
We are going to use the above 2 images in creating the rounded button.
Below is the very simple piece of code and i will try to explain each and everything .
border property– The values in
border:30, 30, 30, 30 tells us how many pixels on the left, right, top, bottom are going to be used for the border of the button. If you give border, see for example
border: 150, 150, 150, 150 this will behave wrongly and the reason is that we are picking up a border bigger than the actual image.
Now below is the code implementing my approach:
Note: Widgets are still rectangles. That means that even if you click on the rounded corners, the button still receive the event.
- Python - Rounding button corners in kivy using .kv file
- Python | Button Action in Kivy
- Python | Toggle button in kivy using .kv file
- Floating Action type button in kivy - Python
- Python - Change button color in kivy using .kv file
- Animated Floating Action Button in kivy - Python
- Python - Change kivy button size and position using kv file
- Disable Kivy Button
- Use image as a button in kivy
- Disable kivy button using .kv file
- Add image button using .kv file in kivy
- Change button Color in Kivy
- Change the size and position of button in Kivy
- Text Input box with a verification button in kivy
- Circular (Oval like) button using canvas in kivy (using .kv file)
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.