Book Image

Kivy Cookbook

By : Hugo Solis
Book Image

Kivy Cookbook

By: Hugo Solis

Overview of this book

Table of Contents (16 chapters)
Kivy Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Creating custom events


Another possibility with events is that we can create and customize our own events. There are many situations where we could need a custom event; for example, when we create our own widget. This recipe will create a new event that will fire an action when the label of the button is changed.

Getting ready

Now we need a more complex recipe, and it is useful for the reader to review the recipes first before getting into the content of custom events in Kivy. We will also use the Kv language to design the widgets, so we assume that the reader is familiar with the Kv language or has gone through the first chapter.

How to do it…

Use the following steps to achieve the end goal:

  1. First, in the KV file, declare an empty label and a button using the following code:

    <MyW>:
        Button:
            id: button1
            text: 'Hello'
        Label:
            id: label1
            pos: 200,200
            text: ''
  2. In the Python code, define the widget class.

  3. In the widget class, define the __init__()...