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.
The Button is a Label with associated actions that are triggered when the button is pressed (or released after a click/touch). We can add functions behind the button and style the button.
In this article, we are going to discuss how we can create the buttons using
.kv file. We do a little bit of button styling also and also we define you how to bind a button to a callback.
To use button you must have to import :
import kivy.uix.button as Button
Basic Approach: 1) import kivy 2) import kivyApp 3) import Widget 4) import Button 5) Set minimum version(optional) 6) Create widget class: 1) Arrange a callback 2) Define Callback function 7) create App class 8) create .kv file (name same as the app class): 1) create Widget 2) Create Button 3) Specify requirements 9) return Layout/widget/Class(according to requirement) 10) Run an instance of the class
One of the common problems is how to add functionality to the button. So to add functionality we use
bind() function it binds the function to the button.
bind() creates an event that is send to
One of the most common problems for new Kivy users is misunderstanding how the bind method works, especially amongst newer Python users who haven’t fully formed their intuition about function calls.
The thing is that the bind method doesn’t know about the existence of a function or its arguments, it only receives the result of this function call. As in the given code when the button is pressed it prints that “button pressed” def in the function callback.
Code to implement the above Approach with button action and styling.
.kv file implementation of the Approach
Showing the button action picture:i.e on clicking on button you will get this output
- Python | Working with buttons in Kivy
- Python | Kivy .kv File
- Python | FloatLayout in Kivy using .kv file
- Python | StackLayout in Kivy using .kv file
- Python | AnchorLayout in Kivy using .kv file
- Python | ScreenManager in Kivy using .kv file
- Python | TextInput in kivy using .kv file
- Python | Accordion in kivy using .kv file
- Python | File chooser in kivy
- Python | PageLayout in Kivy using .kv file
- Python | Canvas in Kivy using .kv file
- Python | Animation in Kivy using .kv file
- Python | Drop-down list in kivy using .kv file
- Python | Carousel Widget In Kivy using .kv file
- Python | Toggle button in kivy using .kv file
- Python | Switch widget in Kivy using .kv file
- Python | Spinner widget in Kivy using .kv file
- Python | Popup widget in Kivy using .kv file
- Python | Progressbar widget in kivy using .kv file
- Python | Adding image 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.