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:
In the KV file, define a label using the following code:
<MyW>: Label: id: label1 pos: 200,200 text: 'Hello'
In the Python file, create the
on_touch_down()
method to add a button every time that a touch is performed.Inside the same method, when double tap is detected, traverse the children property with
self.children
and print the child in the console.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...