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

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:

  1. In the KV file, define BoxLayout to accommodate the buttons.

  2. Define a screen manager widget.

  3. In the Mbutton rule, define the on_press action that will change the current widget in the screen manager as follows:

    <MyW>:
        orientation: 'vertical'
        BoxLayout...