Using swappable widgets
There are some situations wherein you need to shift a widget, but then you realize that it is not necessary to define a new class for it. This is what we call the dynamics class, which allows us to work with different widgets. In this recipe, we want to create some buttons from a list that will swap a label in the app. Here, we will be using the screen manager widget.
Getting ready
This recipe is based on some advanced KV concepts, thus it is advisable to read the recipe Reusing styles in multiple widgets from Chapter 1, Kivy and the Kv language. Also go through the previous recipe Manipulating the widgets tree to understand the differences between them.
How to do it…
To complete this task, follow the next steps:
In the KV file, define
BoxLayout
to accommodate the buttons.Define a screen manager widget.
In the
Mbutton
rule, define theon_press
action that will change the current widget in the screen manager as follows:<MyW>: orientation: 'vertical' BoxLayout...