Python | AnchorLayout in Kivy using .kv file

Kivy is a platform independent GUI tool in Python. As it can be run on Android, IOS, linux and Windows etc. It is basically used to develop the Android application, but it does not mean that it can not be used on Desktops applications.

AnchorLayout:

The AnchorLayout aligns its children to a border (top, bottom, left, right) or center. The class given below is used to implement the anchor layout.

 kivy.uix.anchorlayout.AnchorLayout

AnchorLayout can be initialized with parameters:



anchor_x
Parameters can be passed: “left”, “right” and “center”.

anchor_y
Parameters can be passed:“top”,  “bottom” and “center”.

to select the place where the widgets are placed in the parent container.

There are 9 different layout regions where the Anchorlayout can be placed for effect:

Top-left, top-center, top-right, center-left, center-center, center-right, bottom-left, bottom-center and bottom-right.

Basic Approach:

1) import kivy
2) import kivyApp
3) import gridlayout(not necessary according to requirement)
4) import Anchorlayout
5) Set minimum version(optional)
6) create Layout class
7) create App class
8) Set up .kv file
9) Return the instance of layout class
10) Run an instance of the Appclass

In the below example code, we have used a GridLayout as our root widget class. The GridLayout, will be the parent of 9 AnchorLayouts. The 9 AnchorLayouts will be anchored at the 9 different anchoring positions i.e we are using all the 9 Anchorlayout positions in a single program with the help of 9 Buttons.
 
Implementation of the Approach:

main.py file –

filter_none

edit
close

play_arrow

link
brightness_4
code

# Sample Python application demonstrating the
# working of AnchorLayout in Kivy using .kv file
  
###################################################
  
# base Class of your App inherits from the App class.  
# app:always refers to the instance of your application  
from kivy.app import App
  
# GridLayout arranges children in a matrix.
# It takes the available space and
# divides it into columns and rows, 
# then adds widgets to the resulting “cells”.
from kivy.uix.gridlayout import GridLayout
  
# to change the kivy default settings
# we use this module config
from kivy.config import Config
   
# 0 being off 1 being on as in true / false
# you can use 0 or 1 && True or False
Config.set('graphics', 'resizable', True)
   
  
# creating the root widget used in .kv file 
class Anchor_Layout(GridLayout):
    pass
  
# class in which name .kv file must be named Anchor_Layout.kv   
class Anchor_LayoutApp(App):
    def build(self):
        # returning the instance of root class
        return Anchor_Layout()
  
# run the app
if __name__=='__main__':
    Anchor_LayoutApp().run()

chevron_right


.kv file –

filter_none

edit
close

play_arrow

link
brightness_4
code

# Implementation of .kv file of Anchor layout
  
################################################
  
  
# creating the features of Button
<MyButton@Button>:
    size_hint: [None, None]
    size: [100, 100]
  
# creating the root of .kv
<Anchor_Layout>:
  
    # Assigning grids
    rows: 3
  
    # Anchor Layout 1
    AnchorLayout:
          
        # position of Anchor Layout 
        anchor_x: 'left'
        anchor_y: 'top'
  
        # creating Canvas 
        canvas:
            Color:
                rgb: [.5, .324, .384]
            Rectangle:
                pos: self.pos
                size: self.size
  
        # creating Button
        MyButton:
            text: 'B1'
  
    # Anchor Layout 2
    AnchorLayout:
        anchor_x: 'center'
        anchor_y: 'top'
        canvas:
            Color:
                rgb: [.5, .692, .498]
            Rectangle:
                pos: self.pos
                size: self.size
        MyButton:
            text: 'B2'
  
    # Anchor Layout 3
    AnchorLayout:
        anchor_x: 'right'
        anchor_y: 'top'
        canvas:
            Color:
                rgb: [.5, .692, 1]
            Rectangle:
                pos: self.pos
                size: self.size
        MyButton:
            text: 'B3'
  
    # Anchor Layout 4
    AnchorLayout:
        anchor_x: 'left'
        anchor_y: 'center'
        canvas:
            Color:
                rgb: [.789, .5, .699]
            Rectangle:
                pos: self.pos
                size: self.size
        MyButton:
            text: 'B4'
  
    # Anchor Layout 5
    AnchorLayout:
        anchor_x: 'center'
        anchor_y: 'center'
        canvas:
            Color:
                rgb: [.333, .5, .673]
            Rectangle:
                pos: self.pos
                size: self.size
        MyButton:
            text: 'B5'
              
    # Anchor Layout 6
    AnchorLayout:
        anchor_x: 'right'
        anchor_y: 'center'
        canvas:
            Color:
                rgb: [.180, .5, .310]
            Rectangle:
                pos: self.pos
                size: self.size
        MyButton:
            text: 'B6'
  
    # Anchor Layout 7
    AnchorLayout:
        anchor_x: 'left'
        anchor_y: 'bottom'
        canvas:
            Color:
                rgb: [.180, .398, .5]
            Rectangle:
                pos: self.pos
                size: self.size
        MyButton:
            text: 'B7'
  
    # Anchor Layout 8
    AnchorLayout:
        anchor_x: 'center'
        anchor_y: 'bottom'
        canvas:
            Color:
                rgb: [.438, .329, .5]
            Rectangle:
                pos: self.pos
                size: self.size
        MyButton:
            text: 'B8'
  
    # Anchor Layout 9
    AnchorLayout:
        anchor_x: 'right'
        anchor_y: 'bottom'
        canvas:
            Color:
                rgb: [.611, .021, .5]
            Rectangle:
                pos: self.pos
                size: self.size
        MyButton:
            text: 'B9'      

chevron_right


Output:



My Personal Notes arrow_drop_up

Competative Programmer

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : ManasChhabra2



Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.