Skip to content
Related Articles

Related Articles

Add image button using .kv file in kivy

View Discussion
Improve Article
Save Article
  • Last Updated : 28 Jun, 2022

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

 

Kivy Tutorial – Learn Kivy with Examples.

Image used in this article – 
normal.png: 
 

down.png: 
 

main.py file 
 

Python3




## Sample Python application demonstrating that  
## how to create button using image in kivy using .kv file
     
##################################################     
# import kivy module
import kivy
   
# this restrict the kivy version i.e
# below this kivy version you cannot
# use the app or software
kivy.require("1.9.1")
   
# base Class of your App inherits from the App class.
# app:always refers to the instance of your application
from kivy.app import App
 
# module consist the floatlayout
# to work with FloatLayout first
# you have to import it
from kivy.uix.floatlayout import FloatLayout
 
# creates the button in kivy
# if not imported shows the error
from kivy.uix.button import Button
 
# this restrict the kivy version i.e  
# below this kivy version you cannot  
# use the app or software  
kivy.require('1.9.0'
    
# to change the kivy default settings we use this module config
from kivy.config import Config
    
# 0 being off 1 being on as in true / false
# you can use 0 or 1 && True or False
Config.set('graphics', 'resizable', True)
 
# creating the root widget used in .kv file
class Base(FloatLayout):
 
    # Adding functionality and arranging a callback to a button
    def __init__(self, **kwargs):
        super(Base, self).__init__(**kwargs)
 
    def say_hello(self):
        print("hello")
 
# class in which we are creating the imagebutton
# in .kv file to be named Btn.kv
class BtnApp(App):
    # defining build()
    def build(self):
        # returning the instance of root class
        return Base()
 
# run function runs the whole program
# i.e run() method which calls the target
# function passed to the constructor.
if __name__ == "__main__":
    BtnApp().run()
  

.kv file: 
 

Python3




#.kv file implementation of setting position, size and functionality of btn  
 
 
# create a fully styled functional button
# Adding images normal.png and down.png
<Base>:
    Button:
        text: 'Hit me !!'
        background_normal: 'normal.png'
        background_down: 'down.png'
        size_hint: .3, .3
        pos_hint: {"x":0.35, "y":0.3}
        on_press: root.say_hello()

Output :
When button not pressed 
 

When button is pressed 
 

 


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!