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

Defining widget events


Now, we will study the events that are called widget-defined events. These kinds of events are inherent to the widget. This recipe will illustrate it with probably the most common widget—the button. We will perform two events: one when the button is pressed and the other when the button state is changed.

Getting ready

In this recipe, it is necessary to clear the difference between the state of the button and press action. Because the states of the button are not pressed and no pressed, they actually are normal and down. The press action changes between both states of the button.

How to do it…

To accomplish the end goal, follow these steps:

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

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

  3. In the widget class, define constructor __init__() within a button.

  4. Bind the callback methods to the button.

  5. Define the state_callback and on_press_callback...