Python| AnchorLayout in Kivy - GeeksforGeeks

06 Feb, 2020
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.
 Kivy Tutorial – Learn Kivy with Examples.
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.
Note: Remember adding multiple widgets to an anchor layout, only positions the widgets at the same location.
Basic Approach:

1) import kivy
2) import kivyApp
4) import Anchorlayout
5) Set minimum version(optional)
6) create App class
7) return Layout/widget/Class(according to requirement)
8) Run an instance of the class
 Implementation of the Approach (with some Styling):
1) anchor_x=’right’, anchor_y=’bottom’:
# Sample Python application demonstrating# the working of AnchorLayout in Kivy  # Module imports  # base Class of your App inherits from the App class. # app:always refers to the instance of your application from kivy.app import App  # The AnchorLayout aligns its children to a border# (top, bottom, left, right) or centerfrom kivy.uix.anchorlayout import AnchorLayout  # BoxLayout arranges children in a vertical or horizontal box.# or help to put the childrens at the desired location.from kivy.uix.boxlayout import BoxLayout  # creates the button in kivy # if not imported shows the errorfrom kivy.uix.button import Button    # A Kivy app demonstrating the working of anchor layoutclass AnchorLayoutApp(App):          def build(self):                    # Anchor Layout1        layout = AnchorLayout(        anchor_x ='right', anchor_y ='bottom')        btn = Button(text ='Hello World',                     size_hint =(.3, .3),                     background_color =(1.0, 0.0, 0.0, 1.0))              layout.add_widget(btn)        return layout   # creating the object root for AnchorLayoutApp() class  root = AnchorLayoutApp()# Run the Kivy approot.run()
Output:
If you want to change the positions of the AnchorLayouts then just replace the class code in the above code with below or you can change the anchor_x and anchor_y with any of the parameters to make any 9 combinations as described above.
2) anchor_x=’right’, anchor_y=’top’:
# A Kivy app demonstrating the working of anchor layoutclass AnchorLayoutApp(App):          def build(self):                  # Anchor Layout1        layout = AnchorLayout(        anchor_x ='right', anchor_y ='top')        btn = Button(text ='Hello World',                     size_hint =(.3, .3),                     background_color =(1.0, 0.0, 1.0, 1.0))              layout.add_widget(btn)        return layout 
Output:
3) anchor_x=’center’, anchor_y=’top’:Output:
4) anchor_x=’left’, anchor_y=’top’:Output:
5) anchor_x=’left’, anchor_y=’bottom’:Output:
6) anchor_x=’left’, anchor_y=’center’:Output:
7) anchor_x=’center’, anchor_y=’center’:Output:
8) anchor_x=’center’, anchor_y=’bottom’:Output:
9) anchor_x=’right’, anchor_y=’center’:Output:
ManasChhabra2
Python-gui
Python-kivy
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Read JSON file using Python
Adding new column to existing DataFrame in Pandas
Python map() function
How to get column names in Pandas dataframe
Read a file line by line in Python
How to Install PIP on Windows ?
Enumerate() in Python
Different ways to create Pandas Dataframe
Iterate over a list in Python
Python String | replace()