Python - Working with buttons in Kivy

Kivy is a platform independent GUI tool in Python. As it can be run on Android, IOS, Linux and Windows etc. Kivy provides you the functionality to write the code for once and run it on different platforms. It is basically used to develop the Android application, but it does not mean that it cannot be used on Desktops applications.

The Button is a Label with associated actions that are triggered when the button is pressed (or released after a click/touch). We can add functions behind the button and style the button.


# import kivy module
import kivy  
# this restrict the kivy version below this kivy version you cannot
# use the app or software
# base Class of your App inherits from the App class.
# app:always refers to the instance of your application
from import App  
# creates the button in kivy if not imported shows the error
from kivy.uix.button import Button  
# class in which we are creating the button
class ButtonApp(App):      
   def build(self):
      # use a (r, g, b, a) tuple
      btn = Button(text ="Push Me !",
         font_size ="20sp",
         background_color =(1, 1, 1, 1),
         color =(1, 1, 1, 1),
         size =(32, 32),
         size_hint =(.2, .2),
         pos =(300, 250))
      # bind() use to bind the button to function callback
      btn.bind(on_press = self.callback)
      return btn
   # callback function tells when button pressed
   def callback(self, event):
      print("button pressed")
# creating the object root for ButtonApp() class
root = ButtonApp()  
#run function runs the whole program. run() method which calls the #target function passed to the constructor.