Table of contents
1.
Introduction
2.
Creating Anchor Layout
2.1.
Example
2.2.
anclayout.py
2.3.
AncLayout.kv
2.3.1.
Output
3.
Frequently Asked Questions
3.1.
What is bind()?
3.2.
What is kivy.uix?
3.3.
What is .kv file?
3.4.
What does root mean in Kivy?
4.
Conclusion
Last Updated: Mar 27, 2024
Easy

Anchor Layout using .kv File

Introduction

Kivy is an independent GUI tool in Python that can be used to create Android, IOS, Linux, and Windows applications. Kivy provides the functionality to write the code for once and run it on different platforms. Though, most applications built on kivy are Android applications we can use it for desktop applications as well.

Anchor Layout basically defines a layout by where it is in regard to the border of the parent. Thus mostly it is either with either of the borders or with neither border making a total of 3 options for each axis and is great when we don't need to make a separate layout for each element.

Therefore, in this blog, we will learn how we can implement Anchor Layout using a .kv file.

Creating Anchor Layout

The anchor layout is implemented using the kivy.uix.anchorlayout.AnchorLayout class. AnchorLayout can be initialized with the parameters anchor_x and anchor_y.

The parameters take the following values:

  • anchor_x: “left”, “right”, and “center”. Default value being “center”
  • anchor_y: “top”, “bottom”, and “center”. Default value being “center”

 

There are two other parameters in the Anchor Layout Class as well:

  • do_layout(*largs): this function is used when the layout is called by trigger and can be used to schedule the children in the next frame. 
  • padding: This defines the padding between the widget and the children in pixels and can be represented in 3 ways: 
    [padding_left, padding_top, padding_right, padding_bottom], 
    [padding_horizontal, padding_vertical] and 
    [padding]. 
    The default value of this property is [0, 0, 0, 0].

 

As there are 9 different layouts taking all possible combinations we will show all the 9 layouts now. We will show all the combinations using the grid layout for a 3X3 grid.

Example

In the below example, we first make a GridLayout root widget class which will be the parent of 9 AnchorLayouts and the 9 different anchoring positions with the help of buttons.

anclayout.py

import kivy 

from kivy.app import App 
 
#first we import all the necessary dependencies here we will need the grid layout for the overall layout, the config to set graphics as resizable

from kivy.uix.gridlayout import GridLayout
  
from kivy.config import Config 

Config.set('graphics', 'resizable', True) 
  
#we create the outer parent element of the layout. We need no content here as we will do it in the .kv file
class AncLayout(GridLayout): 
    pass
  
#we create a function that will fetch the respective .kv file and is run.
class AncLayoutApp(App): 
    # defining build() 
    def build(self): 
        # returning the instance of root class 
        return AncLayout() 
  
# run the app 
if __name__ == "__main__": 
    AncLayoutApp().run() 

AncLayout.kv

In the kv file first, we will define the button we are going to use. Next, we need to create the 9 boxes that are played in an anchor layout in all the combinations. So the overall widget is a 3X3 grid which has anchor layout of children elements within them with different colors.

<Button>:
        font_size: 15
        size_hint: 0.25, 0.25
  
<AncLayout>:

        rows: 3  
 
    # Top-Left Anchor 
    AnchorLayout:
          
        # position of Anchor Layout 
        anchor_x: 'left'
        anchor_y: 'top'
  
        # creating Canvas 
        canvas:
            Color:
                rgb: [0, 0, 0]
  
        # creating Button
        Button:
            text: 'Hello1'
  
    # Top-Center Anchor
    AnchorLayout:
        anchor_x: 'center'
        anchor_y: 'top'
        canvas:
            Color:
                rgb: [0, 0, 1]

        Button:
            text: 'Hello2'
  
    # Top-Right Anchor
    AnchorLayout:
        anchor_x: 'right'
        anchor_y: 'top'
        canvas:
            Color:
                rgb: [0, 1, 0]

        Button:
            text: 'Hello3'
  
    # Center-Left Anchor
    AnchorLayout:
        anchor_x: 'left'
        anchor_y: 'center'
        canvas:
            Color:
                rgb: [0, 1, 1]

        Button:
            text: 'Hello4'
  
    # Center-Center Anchor
    AnchorLayout:
        anchor_x: 'center'
        anchor_y: 'center'
        canvas:
            Color:
                rgb: [1, 0, 0]

        Button:
            text: 'Hello5'
              
    # Center-Right Anchor
    AnchorLayout:
        anchor_x: 'right'
        anchor_y: 'center'
        canvas:
            Color:
                rgb: [1, 0, 1]

        Button:
            text: 'Hello6'
  
    # Bottom-Left Anchor
    AnchorLayout:
        anchor_x: 'left'
        anchor_y: 'bottom'
        canvas:
            Color:
                rgb: [1, 1, 0]

        Button:
            text: 'Hello7'
  
    # Bottom-Center Anchor
    AnchorLayout:
        anchor_x: 'center'
        anchor_y: 'bottom'
        canvas:
            Color:
                rgb: [1, 1, 1]
  
        Button:
            text: 'Hello8'
  
    # Bottom-Right Anchor
    AnchorLayout:
        anchor_x: 'right'
        anchor_y: 'bottom'
        canvas:
            Color:
                rgb: [1, 1, .5]

        Button:
            text: 'Hello9' 

Output

Frequently Asked Questions

What is bind()?

It is a function that binds a callback to a particular event on an element. It takes two parameters, the callback and the event as input.

 

What is kivy.uix?

kivy.uix is a module that contains classes for creating and managing Widgets for creation in any Application.

 

What is .kv file?

A .kv file helps create a widget tree and the explicit declaration of bindings.

 

What does root mean in Kivy?

In a kv file, the root always refers to a parent with angle brackets and thus there can be multiple roots. 

Conclusion

In this blog, we discussed how we can create an Anchor Layout in Kivy.

 

You may want to learn more about Kivy here, float layout in kivy here and about Button Action in Kivy here We hope that this blog has helped you enhance your knowledge regarding verifying using Selenium. Do upvote our blog to help other ninjas grow.

 

Learning never stops, and to feed your quest to learn and become more skilled, head over to our practice platform Coding Ninjas Studio to practice top problems, attempt mock tests, take on guided paths, read interview experiences, try our interview bundle and much more.!
 

Happy Learning!

Live masterclass