Table of contents
1.
Introduction
2.
Creating an Image Button
2.1.
Example
2.1.1.
Output
3.
Frequently Asked Questions
3.1.
What is bind()?
3.2.
What is kivy.uix?
3.3.
What is .kv file?
3.4.
What does root mean in Kivy?
4.
Conclusion
Last Updated: Mar 27, 2024
Easy

Image as a Button in Kivy

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

We know how important a button is, however in some cases we find it better to describe a button using an image rather than simple text. Here, we are able to do this with the help of buttons in kivy as well. In this blog, we will learn about how we can bring into reality what we discussed earlier using the button module from kivy.uix module.

Creating an Image Button

The basic approach here would be to import the kivy and the kivy app and then create a class to extend and create a button with a background as an image. For the image, we will store two images back1.png and back2.png. We have 2 images to have two images when the image is clicked and released respectively. 

Before we start coding we need to learn about the three properties of the button that define the background of any button created using kivy:

  • background_down: Used to specify the default background image when the button is pressed and has one parameter of string type passed into it.
  • background_normal: Used for the default background when the button is not pressed and also contains a string parameter.
  • background_disabled_normal: When the button is disabled and not pressed this property specifies the graphical background of the button.

Also Read - Image Sampling

Now we code the example:

Example

import kivy 
    
from kivy.app import App 
    
from kivy.uix.button import Button
  
kivy.require('1.9.0')   


#our class to create a button and execute it
class ButtonApp(App): 
        
    def build(self): 
  
        # create an image a button 
        # Adding images back1.png image as button
        # Adding image back2.png as the background when clicked
        testBtn = Button(text ="Hello World !",
                    background_normal = 'back1.png',
                    background_down ='back2.png'
                  ) 
      
        return testBtn 
            
    
# binding ButtonApp to a root
root = ButtonApp() 
    
root.run()
You can also try this code with Online Python Compiler
Run Code

Now that we have successfully created an image button, we need to add some functionality to it now. For that, we use the bind() function and bind a callback to the on-click event.

def build(self): 
  
        # create a fully styled functional button
        testBtn = Button(text ="Hello World !",
                    background_normal = 'back1.png',
                    background_down = 'back2.png'
                  ) 
    
        # bind() use to bind the button to function callback 
        testBtn.bind(on_press = self.callback) 
        return testBtn 
    
    # callback function tells when button pressed 
    def callback(self, event): 
        print("Hello World Again")  
You can also try this code with Online Python Compiler
Run Code

Output

Frequently Asked Questions

What is bind()?

It is a function that binds a callback to a particular event on an element. It takes two parameters, the callback, and the event, as input.

What is kivy.uix?

kivy.uix is a module that contains classes for creating and managing Widgets for creation in any Application.

What is .kv file?

A .kv file helps create a widget tree and the explicit declaration of bindings.

What does root mean in Kivy?

In a kv file the root always refers to a parent with angle brackets and thus there can be multiple roots. 

Conclusion

In this blog, we discussed how we can create Buttons and bind functions to them using Kivy.

You may want to learn more about Kivy hereWe hope that this blog has helped you enhance your knowledge regarding verifying using Selenium. Do upvote our blog to help other ninjas grow.

Learning never stops, and to feed your quest to learn and become more skilled, head over to our practice platform Coding Ninjas Studio to practice top problems, attempt mock tests, take on guided paths, read interview experiences, try our interview bundle and much more.!

Happy Learning!

Live masterclass