Kivy Tutorials
About Lesson

In this Kivy Tutorial we are going to talk about Kivy Button with Callbacks, we will learn that how you can create button in kivy and how you can connect button with the callbacks in kivy. kivy Button is a Label with associated actions that are triggered when the button is pressed (or released after a click/touch). To configure the button, the same properties (padding, font_size, etc) and sizing system are used as for the Label class.



There are two ways that you can create button in kivy, the first way is that you can create button using the kivy.uix.button module, and the second way is using the kivy design language, so first let’s just do the first way. also iam going to show you how to create callbacks for the kivy button.



This is the complete code for Kivy Tutorial – Kivy Button with Callbacks



In the code first we have created our class that extends from the App class, after that we have created a button, and you need to give some parameters, like the text of the button size and position of the button.



This code is for binding of our button with the click_me() method.



This is the method that we want when a user clicks on the button, we are going to just print something in the console.





Run the complete code and this is the result.

Kivy Tutorial - Kivy Button with Callbacks
Kivy Tutorial – Kivy Button with Callbacks

Creating Button with KV Language

So now let’s create our button using kivy design language, this is our complete code for creating button using Kivy design language.

So you can see at the top i have created a new class, that class extends from Widget base class, now it is good idea to create our Widget class.  and for right now i have not added anything in this class, but we will add our callback for this button in this class, also we have returned this class in our main window class.




And this is our kivy file, make sure that your kivy file name should be buttonwindow.kv, because our main class is ButtonWindow.

So in the kivy file first of all we have added the kivy version, after that we are going to write the class that we want to define rules, in our case it is MyWidget. and we have added our button with some attributes like position, size, color and font size. also pay attention in your kivy file indentation.




Run the complete code and this is the result.

Kivy Button with Callbacks
Kivy Button with Callbacks

Adding Callbacks to Kivy Button

Now let’s add callbacks for our button, let’s change our python file and add a new method in our MyWidget class like this for our callback. we will connect this method in our kivy file with our button.

We have added a method of click_me() in out class, basically using this method when a user clicks on the kivy button i want to change the text of the label.



Now this is our updated kivy file. so the new thing in this file is that we have added a new widget of label, also we have added an id to the label, after that you need to reference the label in our MyWidget rules. also you can see we have a new thing in our Button, that is on_press.



Using on_press we have connected our kivy code with our method in the Python code.




Run the complete code and this is the result.

Kivy Tutorial
Kivy Tutorial