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 AnchorLayout


AnchorLayout aligns the children to a border or center. In this recipe, we will learn to align two buttons in a border.

How to do it…

In this recipe, follow the next steps:

  1. In the KV file, provide the anchor_x and anchor_y properties.

  2. Define two buttons using the following code:

    <MyW>:
        anchor_x: 'right'
        anchor_y: 'bottom'
        Button:
            id: label1
            size_hint: .2, .2
            text: 'B1'
        Button:
            id: label2
            size_hint: .1, .1
            text: 'B2'
  3. In the Python file, define a class as an AnchorLayout reference for the rule of the KV file using the following code:

    import kivy
    
    from kivy.app import App
    from kivy.uix.anchorlayout import AnchorLayout from kivy.clock import Clock
    
    class MyW(AnchorLayout):
        pass
    class e11App(App):
        def build(self):
            return MyW()
    
    if __name__ == '__main__':
        e11App().run()

How it works…

The second and third lines of the KV file are the properties that are defined with respect to which border we will...