The Wayback Machine - https://web.archive.org/web/20241127032413/https://www.geeksforgeeks.org/python-working-with-buttons-in-kivy/
Open In App

Python | Working with buttons in Kivy

Last Updated : 18 Jul, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

Kivy is a platform-independent GUI tool in Python as it can be run on Android, IOS, Linux 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 can not be used on desktop applications.
 

 Kivy Tutorial – Learn Kivy with Examples.

Now in this article, we will learn how to build a button in Kivy, just like the button we use in calculators and many more places, adding functionality to the button, and styling of the button.
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.
 

Basic Approach to follow while creating button : 

-> import kivy
-> import kivy App
-> import button
-> set minimum version(optional)
-> Extend the class
-> Add and return a button
-> Run an instance of the class


Code #1: How to create a button in Kivy. 
 

Python3
# 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

# 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):
        
        btn = Button(text ="Push Me !")
        return btn

# creating the object root for ButtonApp() class 
root = ButtonApp()

# run function runs the whole program
# i.e run() method which calls the
# target function passed to the constructor.
root.run()

Output: 
 


In the above output, the button will cover the whole window as we have not given any specific size or done any styling in the button so by default it shows button size equal to window size. 
  
Code #2: Styling The Button
 

Python
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=(None, None),
                   pos =(300, 250))

        return btn

Output: 


 


These are just similar to the HTML, CSS effects. By this, we fix the position of a button at the center of the window, text size, colour and anything you want. 
  
Code #3 Adding Functionality behind the button.


One of the common problems is how to add functionality to the button. So to add functionality we use 
bind() function it binds the function to the button. bind() creates an event that is send to callback().
One of the most common problems for new Kivy users is misunderstanding how the bind method works, especially amongst newer Python users who haven’t fully formed their intuition about function calls. 
The thing is that the bind method doesn’t know about the existence of a function or its arguments, it only receives the result of this function call. As in the given code when the button is pressed it prints that “button pressed” def in the function callback.
In the given code inside bind() we use on_press because when button press it tells the function that button is pressed then the bind uses its functionality. 
 

Python
# 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

# 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=(None, None),
                   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")
        print('Yoooo !!!!!!!!!!!')
        

# creating the object root for ButtonApp() class 
root = ButtonApp()

# run function runs the whole program
# i.e run() method which calls the target
# function passed to the constructor.
root.run()

Output: 
 


Video output: 
 

Button using KivyMD:

KivyMD is an extension of the Kivy framework. KivyMD is a collection of Material Design widgets for use with Kivy, a GUI framework for making mobile applications.

Below is an example of MDRectangularFlatButton

MDRectangleFlatButton has the following parameters:

  • text- the text we want to put on the button
  • pos_hint- a dictionary having the position with respect to x-axis and y-axis
  • on_release- it is a function that has the properties that we want to call on clicking the button
Python
from kivymd.app import MDApp
from kivymd.uix.screen import Screen
from kivymd.uix.button import MDRectangleFlatButton

class Demo(MDApp):

    def build(self):
        screen = Screen()
        
        btn= MDRectangleFlatButton(text="Submit",pos_hint={'center_x':0.5,'center_y':0.3},on_release=self.btnfunc)
           screen.add_widget(btn) 
        # adding widgets to screen
    
        return screen
    def btnfunc(self,obj):
        print("button is pressed!!")
   
if __name__=="__main__":
    Demo().run()

 Output:


When the button is pressed it displays the following output



Previous Article
Next Article

Similar Reads

Python | Working with buttons in Kivy with .kv file
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. Button: The Button is a Label with associated actions that are triggered when the button is pressed (or released a
4 min read
Python | Kivy .kv File
As when we write the application in Python kivy, to write all the things on the same code make a mess in the code and it is hard to understand that by someone another. Also writing a large code makes hard to maintain the construction of the widget tree and explicit the declaration of bindings.The KV language, allows us to create own widget tree in
3 min read
Introduction to Kivy ; A Cross-platform Python Framework
Kivy is an open-source, cross-platform Python framework used for developing multi-touch applications with a natural user interface. It allows developers to build applications that run on multiple platforms, including Windows, macOS, Linux, iOS, and Android. Kivy is based on the Model-View-Controller (MVC) architecture and uses a declarative languag
4 min read
Python | Ellipse (different polygons) 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. Kivy Tutorial - Learn Kivy with Examples. Ellipse: Ellipse is a vertex canvas instruction. It allows drawing of a
4 min read
Python | Scrollview widget 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 Desktop applications. Kivy Tutorial - Learn Kivy with Examples. Scroll view: The ScrollView widget provides a scrollable/pannable viewp
3 min read
Python | Carousel Widget 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. 👉🏽 Kivy Tutorial - Learn Kivy with Examples. Carousel widget: The Carousel widget provides the cla
3 min read
Python | Float Layout 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. 👉🏽 Kivy Tutorial - Learn Kivy with Examples. FloatLayout: Floatlayout allows us to place the eleme
5 min read
Python | Make a simple window using kivy
Kivy is a platform independent 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 can not be used on Desktops applications. Use this command To install kivy: pi
5 min read
GridLayouts in Kivy | Python
Kivy is a platform independent 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 can not be used on Desktop applications. Use this command To install kivy: pi
3 min read
Python | Add Label to a kivy window
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 Desktop applications.Label widget - The Label widget is for rendering text. It supports ASCII and unicode strings. The label is the text
4 min read
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 Desktop applications. ???????? Kivy Tutorial - Learn Kivy with Examples. Now in this article, we will learn how to build a button in kiv
3 min read
Python | Window size Adjustment 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 can not be used on Desktops applications.Kivy is that plat
4 min read
Python | BoxLayout widget 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. ???????? Kivy Tutorial - Learn Kivy with Examples. Now in this article, we will learn about the use of BoxLayout
5 min read
Python | Slider widget 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. ???????? Kivy Tutorial - Learn Kivy with Examples. Slider: To work with the slider you first have to import the mo
4 min read
Python | Checkbox widget in Kivy
Kivy is a platform independent GUI tool in Python. Kivy applications 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 Desktop applications. Kivy Tutorial - Learn Kivy with Examples. Checkbox widget -CheckBox is a specific two-state button th
4 min read
Python | Multiple Sliders widgets Controlling Background Screen or WindowColor in Kivy
Prerequisite - Slider in KivyKivy 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.In this article, we will learn How we can control the background color in kivy that me
3 min read
Python | AnchorLayout in Kivy using .kv file
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. 👉🏽 Kivy Tutorial - Learn Kivy with Examples. AnchorLayout: The AnchorLayout aligns its children to
4 min read
Python| AnchorLayout 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. 👉🏽 Kivy Tutorial - Learn Kivy with Examples. AnchorLayout: The AnchorLayout aligns its children to
3 min read
Python | StackLayout 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. ???????? Kivy Tutorial - Learn Kivy with Examples. StackLayout: To use StackLayout first import StackLayout by bel
3 min read
Python | StackLayout in Kivy using .kv file
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. ???????? Kivy Tutorial - Learn Kivy with Examples. StackLayout: To use StackLayout first import StackLayout by bel
3 min read
Python | FloatLayout in Kivy using .kv file
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. 👉🏽 Kivy Tutorial - Learn Kivy with Examples. FloatLayout: Floatlayout provides us the flexibility
3 min read
Python | Relative Layout 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 Desktop applications. Kivy Tutorial - Learn Kivy with Examples. Relative Layout:Relative layout is just similar to the FloatLayout the d
5 min read
Python | Relative Layout in Kivy using .kv file
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. ???????? Kivy Tutorial - Learn Kivy with Examples. Relative Layout: This layout operates in the same way as FloatL
4 min read
Python | Grid Layout in Kivy without .kv file
Kivy is a platform independent 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 can not be used on Desktops applications. ???????? Kivy Tutorial - Learn Kivy
5 min read
Python | PageLayout 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. Kivy Tutorial - Learn Kivy with Examples. PageLayout: The PageLayout works in a different manner from other layo
4 min read
Python | PageLayout in Kivy using .kv file
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. Kivy Tutorial - Learn Kivy with Examples. PageLayout: The PageLayout works in a different manner from other layout
5 min read
Python | Layouts in layouts (Multiple Layouts) 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.In this article, we are going to discuss how we can use layouts in layouts basically multiple Layouts the most basi
5 min read
Python | Add image widget 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 Desktop applications. 👉🏽 Kivy Tutorial - Learn Kivy with Examples. Image Widget: The Image widget is used to display an
4 min read
Python | Adding image in Kivy using .kv file
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. Kivy Tutorial - Learn Kivy with Examples. Image Widget: The Image widget is used to display an image. To use the
4 min read
Python | Popup widget 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. 👉🏽 Kivy Tutorial - Learn Kivy with Examples. Popup widget : The Popup widget is used to create pop
6 min read
Article Tags :
Practice Tags :
three90RightbarBannerImg