Float Layout in Kivy Python (original) (raw)

Last Updated : 12 Jan, 2026

FloatLayout in Kivy is used to place widgets using relative positions and sizes. Widgets are positioned using percentage-based values so they move and resize automatically when the window size changes.

**Example: In this example, a text input box is placed inside a FloatLayout using relative size and position.

Python `

from kivy.app import App from kivy.uix.floatlayout import FloatLayout from kivy.uix.textinput import TextInput

class Main(FloatLayout): def init(self, **kw): super().init(**kw) t = TextInput(text="Enter name", size_hint=(.5, .1), pos_hint={'x': .25, 'y': .45}) self.add_widget(t)

class Demo(App): def build(self): return Main()

Demo().run()

`

**Output

FloatLayout

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

**Explanation:

Syntax

FloatLayout(size_hint=None, pos_hint=None)

**Parameters:

Examples

**Example 1: In this example, a button is placed using fixed pixel coordinates.

Python `

from kivy.app import App from kivy.uix.floatlayout import FloatLayout from kivy.uix.button import Button

class Main(FloatLayout): def init(self, **kw): super().init(**kw) b = Button(text="Click", size_hint=(.3, .2), pos=(200, 150)) self.add_widget(b)

class Demo(App): def build(self): return Main()

Demo().run()

`

**Output

Floatlayout

A button appears at a fixed position on the window.

**Explanation:

**Example 2: In this example, two widgets are placed at different positions using relative coordinates.

Python `

from kivy.app import App from kivy.uix.floatlayout import FloatLayout from kivy.uix.button import Button from kivy.uix.textinput import TextInput

class Main(FloatLayout): def init(self, **kw): super().init(**kw) self.add_widget(TextInput(text="Name", size_hint=(.4, .1), pos_hint={'x': .1, 'y': .6})) self.add_widget(Button(text="OK", size_hint=(.3, .15), pos_hint={'x': .5, 'y': .3}))

class Demo(App): def build(self): return Main()

Demo().run()

`

**Output

Output2

A text box and a button appear at different positions on the screen.

**Explanation: