Python| AnchorLayout in Kivy
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
Anchorlayoutcan 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 center from 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 error from kivy.uix.button import Button # A Kivy app demonstrating the working of anchor layout class 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 app root.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 layout class 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:

Recommended Posts:
- Python | AnchorLayout in Kivy using .kv file
- Python | PageLayout in Kivy
- Python | Accordion in kivy
- Python | Bubble in kivy
- GridLayouts in Kivy | Python
- Python | Scatter in kivy
- Python | Kivy .kv File
- Python | Canvas in kivy
- Python | Animation in Kivy
- Python | StackLayout in Kivy
- Python | File chooser in kivy
- Python | Add image widget in Kivy
- Python | TextInput in kivy using .kv file
- Python | Button Action in Kivy
- How to use multiple UX Widgets in kivy | Python
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


