AnchorLayout in Kivy Python (original) (raw)

Last Updated : 12 Jan, 2026

AnchorLayout places widgets at a fixed position inside a window using horizontal (anchor_x) and vertical (anchor_y) alignment. It is used when you want a widget to stay at a specific side or center of the screen even when the window size changes.

**Example: In this example, this program displays a TextInput box placed at the center of the Kivy window using AnchorLayout.

Python `

from kivy.app import App from kivy.uix.anchorlayout import AnchorLayout from kivy.uix.textinput import TextInput

class MyApp(App): def build(self): l = AnchorLayout(anchor_x='center', anchor_y='center') t = TextInput(text="Enter text", size_hint=(0.5, 0.2)) l.add_widget(t) return l

MyApp().run()

`

**Output

AnchorLayout

A text input box appears at the center of the window.

**Explanation:

Syntax

AnchorLayout(anchor_x='center', anchor_y='center')

**Parameters:

Examples

**Example 1: In this example, this program places a button at the bottom-right corner of the window.

Python `

from kivy.app import App from kivy.uix.anchorlayout import AnchorLayout from kivy.uix.button import Button

class MyApp(App): def build(self): l = AnchorLayout(anchor_x='right', anchor_y='bottom') b = Button(text="Click", size_hint=(0.3, 0.2)) l.add_widget(b) return l

MyApp().run()

`

**Output

Output1

A button appears at the bottom-right of the window.

**Explanation:

**Example 2: In this example, this program shows a button on the left side while keeping it vertically centered.

Python `

from kivy.app import App from kivy.uix.anchorlayout import AnchorLayout from kivy.uix.button import Button

class MyApp(App): def build(self): l = AnchorLayout(anchor_x='left', anchor_y='center') b = Button(text="Menu", size_hint=(0.3, 0.3)) l.add_widget(b) return l

MyApp().run()

`

**Output

Output2

A button appears on the center-left side of the window.

**Explanation: