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.
As we have discussed earlier that how to work with images and now in this we will gonna be learn how to use the images and create a button with them. So In this article we will learn how can we use the images as the button using the .kv file functionality and also give some styling to the button. But before we start, let’s learn some properties of button –
background_down : Background image of the button used for the default graphical representation when the button is pressed.
background_normal : Background image of the button used for the default graphical representation when the button is not pressed.
background_disabled_normal : Background image of the button used for the default graphical representation when the button is disabled and not pressed.
These all 3 properties are a StringProperty that means they only takes string as values.
To use button you must have to import :
import kivy.uix.button as Button
Basic Approach: 1) import kivy 2) import kivyApp 3) import button 4) import FloatLayout 5) set minimum version(optional) 6) Create the Layout class 7) Create App class 8) Create .kv file: 1) Add Base class 2) Add Button properties 3) Add Image as button 4) Resizing, Positioning, functionality etc of Imagebutton 9) return instance of the layout class 10) Run an instance of the class
Image used in this this article –
When button not pressed
When button is pressed
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course