Python | Working with buttons in Kivy with a .kv file



Kivy — it is a platform independent GUI tool in Python. Since it can run on Android, IOS, Linux, Windows, etc. It is mainly used to develop Android application, but that does not mean that it cannot be used in desktop applications.

Button:

Button — it is a label with associated actions that are triggered when the button is pressed (or released after pressing / tapping). We can add functionality behind the button and style the button.

In this article, we are going to discuss how we can create buttons using the .kv file. We`ll also style the buttons a bit and also define how to bind the button to the callback.

To use the button, you must 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 — how to add functionality to a button. Therefore, to add functionality, we use the bind () function which binds the function to the button.  bind () generates an event that is dispatched to callback () .

One of the most common problems for new Kivy users is a misunderstanding of how the method works bindings, especially among new Python users who have not yet fully formed their concept of function calls. 
The fact is that the bind method does not know about the existence of the function or its arguments, it only receives the result of this function call. As in this code, when the button is clicked, it prints the value of "button clicked" in the callback function.

Code to implement the above approach with buttons and styles.

.kv approach implementation file

# import nimble module

import kivy 

 
# this limits the kivy version ie
# below this version you cannot
# use the application or software

kivy.require ( "1.9.1"

 
# Your application base class inherits from the application class.
# app: always refers to your application instance

from kivy.app import App 

 
# creates a button in kiw
# if not imported shows an error

from kivy.uix.button import Button

 
# Widgets are graphical user elements
# an interface that is part of the user experience.

from kivy. uix.widget import Widget

 

  
# Create a widget class
# through this we add a button
# class commands are in the .kv file

class Button_Widget (Widget):

 

def __ init __ ( self , * * kwargs):

 

# Python super function () allows us

# explicitly refers to the parent class.

 

  super (Button_Widget, self ) .__ init __ ( * * kwargs )

 

# create a button

  btn1 = Button (text = `Hello World 1` , font_size = "15sp" ,

background_color = ( 1 , 1 , 1 , 1 ), 

color = ( 1 , 1 , 1 , 1 ), 

# size = (32, 32),

# size_hint = (. 2, .2),

pos = ( 300 , 250 )) 

  

# Organize a callback for the button using

# bind () function in kiw.

btn1.bind (on_press = self . callback)

self . add_widget (btn1)

 

# function callback it reports when the button is clicked

# It reports the state and instance of the button.

def callback ( self , instance):

print ( " Button is pressed " )

print ( `The button% s state is & lt;% s & gt;` % (instance, instance.state) )

 
# create application class

class ButtonApp (App):

  

def build ( self ):

# return the widget

return Button_Widget ()

 
# run application

if __ name__ = = "__ main__" :

ButtonApp (). run ()

# .kv code file main.py
# Adding a widget button

 
& lt; Button_Widget & gt ;:

 

# define button size

size: 100 , 100

 

# create canvas

canvas.before:

Color:

rgba: 0.72 0.62 , 0.92 , 1

Rectangle:

pos: self . pos

size: self . size

Output:

Display the button action image: i.e. when you click on the button you will get this output