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 RelativeLayout


RelativeLayout is a self-contained layout where the positions are relative to the layout and not with the app window. In this recipe, we will localize two buttons in RelativeLayout, which will be a child of FloatLayout.

Getting ready

The RelativeLayout is similar to the FloatLayout; thus, to spot the difference, it is advisable to go through the Using FloatLayout recipe.

How to do it…

To complete the recipe, perform the following steps:

  1. In the KV file, define two buttons with different positions using the following code:

    <MyW1>:
        Button:
            id: label1
            size_hint: .2, .2
            pos: 150, 150
            text: 'B1'
        Button:
            id: label2
            size_hint: .1, .1
            pos: 30, 30
            text: 'B2'
  2. In the Python file, define a class as a RelativeLayout reference for the rule of the KV file.

  3. Set size and position for the layout.

  4. As shown here, define a subclass of FloatLayout and add the RelativeLayout as a child.

    import kivy
    
    from kivy.app import App...