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

Traversing the tree


There are situations when you need to go through the app's widget tree. This recipe will use a tap to add new buttons to the root widgets and double tap to print the children of the root widget in the console. Again, triple tap will clean the widgets of our app.

Getting ready

The code for this recipe is similar to the previous recipe, so you can refer to them and apply the same to this recipe.

How to do it…

For this recipe, follow the next steps:

  1. In the KV file, define a label using the following code:

    <MyW>:
        Label:
            id: label1
            pos: 200,200
            text: 'Hello'
    
  2. In the Python file, create the on_touch_down() method to add a button every time that a touch is performed.

  3. Inside the same method, when double tap is detected, traverse the children property with self.children and print the child in the console.

  4. Also when a triple tap is detected, use the clear_widgets() method as shown in the following code to clear all the widgets in the app:

    import kivy
    
    from...