Python | Button Action in Kivy

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.

Now in this article, we will learn how to build a button in kivy by using the kv file, just like the button we use in calculators and many more places.

Buttons :
The Button is a Label with associated actions that are triggered when the button is pressed (or released after a click/touch). To bind the action of button when it pressed we have the function on_press.

Basic Approach for the button Actions using .kv file:

1) Import kivy
2) Import kivy app
3) Import Box layout
4) create .kv with label and button
5) Bind button to a method
6) create method
Now let’s code the .py file.

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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
  
# BoxLayout arranges children 
# in a vertical or horizontal box.
from kivy.uix.boxlayout import BoxLayout
  
# class in which we are defining action on click
class RootWidget(BoxLayout):
  
    def btn_clk(self):
        self.lbl.text = "You have been pressed"
  
  
# creating action class and calling 
# Rootwidget by returning it
class ActionApp(App):
  
    def build(self):
        return RootWidget()
  
# creating the myApp root for ActionApp() class  
myApp = ActionApp()
  
# run function runs the whole program 
# i.e run() method which calls the 
# target function passed to the constructor. 
myApp.run()

chevron_right


 
.kv file of the above code [action.kv] :

Must be saved with the name of the ActionApp class. i.e action.kv.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Base widget from Rootwidget class in .py file
<RootWidget>:
  
    # used to change the label text
    # as in rootwidget class
    lbl:my_label
  
    # child that is an instance of the BoxLayout
    BoxLayout:
        orientation: 'vertical'
        size: [1, .25]
  
    Button:
        text:'Click Me'
        font_size:"50sp"
        color: [0, 255, 255, .67]
        on_press: root.btn_clk()
  
    Label:
        # id is limited in scope to the rule
        # it is declared in. An id is a weakref
        # to the widget and not the widget itself.
        id: my_label
        text: 'No click Yet'
        color: [0, 84, 80, 19]

chevron_right


Output:


Video explaning output:

Note: BoxLayout arranges widgets in either in a vertical fashion that is one on top of another or in a horizontal fashion that is one after another.



My Personal Notes arrow_drop_up

Competative Programmer

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.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.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.