Kivy is a platform independent GUI tool in Python. As it can be run on Android, IOS, linux and Windows etc. Kivy provides you the functionality to write the code for once and run it on different platforms. It is basically used to develop the Android application, but it does not mean that it can not be used on Desktops applications.
Now in this article, we will learn how to build a button in kivy, just like the button we use in calculators and many more places, adding functionality to the button, styling of the button.
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.
Basic Approach to follow while creating button :
-> import kivy -> import kivy App -> import button -> set minimum version(optional) -> Extend the class -> Add and return a button -> Run an instance of the class
Code #1: How to create button in kivy.
In the above output, the button will cover the whole window as we have not given any specific size or done any styling in the button so by default it shows button size equal to window size.
Code #2: Styling The Button
These are just similar to the HTML, CSS effects. By this, we fix the position of a button at the center of the window, text size, colour and anything you want.
Code #3 Adding Functionality behind the button.
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 callback().
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.
In the given code inside bind() we use on_press because when button press it tells the function that button is pressed then the bind uses its functionality.
- Python | Working with buttons in Kivy with .kv file
- PyQt5 QCalendarWidget - Setting Border to the tool buttons
- How to create Buttons in a game using PyGame?
- PyQt5 QSpinBox - Checking if the arrow buttons are removed or not
- PyQt5 QSpinBox - Removing the arrow buttons
- PyQt5 QCalendarWidget - Background Color to the tool buttons
- PyQt5 QCalendarWidget – Setting Skin to the tool buttons
- PyQt5 QDateEdit - Removing Buttons from it
- PyQt5 QDateEdit - Making Arrow buttons to left side
- Python | Kivy .kv File
- Introduction to Kivy ; A Cross-platform Python Framework
- Python | Ellipse (different polygons) in Kivy
- Python | Scrollview widget in kivy
- Python | Carousel Widget In Kivy
- Python | Float Layout in Kivy
- Python | Make a simple window using kivy
- GridLayouts in Kivy | Python
- Python | Add Label to a kivy window
- Python | Button Action in Kivy
- Python | Window size Adjustment 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.