Python | FloatLayout in Kivy using .kv file (original) (raw)
Last Updated : 11 Jul, 2025
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.
FloatLayout:
**Floatlayout **provides us the flexibility to arrange the elements like button relatively i.e it allows us to place the elements using something called relative position. This means rather than defining the specific position or the coordinates we will place everything using the percentage w.r.t the size of window i.e we can dynamically arrange the position of the elements. The first thing we need to do import FloatLayout -
from kivy.uix.floatlayout import FloatLayout
We have 2 properties to create the dynamic placement -
- pos_hint: provide hint of position We can define upto 6 keys i.e. it takes arguments in form of dictionary. pos_hint = {"x":1, "y":1, "left":1, "right":1, "top":1, "bottom":1}2) size_hint: provide hint of sizeContains two arguments i.e. width and height
Note:
- You can only use values between 0-1 for both size_hint and pos_hint. Where 0 = 0% and 1 = 100%.
- The coordinate system in kivy works from the bottom left! This will be important when placing our objects. (i.e (0, 0) is the bottom left).
Basic Approach:
- import kivy
- import kivyApp
- import Floatlayout
- Set minimum version(optional)
- create Layout class
- create App class
- Set up .kv file
- return Layout/widget/Class(according to requirement)
- Run an instance of the class
Implementation of the approach - main.py file
Python3 `
Sample Python application demonstrating the
working of FloatLayout in Kivy using .kv file
###################################################
import modules
import kivy
base Class of your App inherits from the App class.
app:always refers to the instance of your application
from kivy.app import App
module consist the floatlayout
to work with FloatLayout first
you have to import it
from kivy.uix.floatlayout import FloatLayout
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 FloatLayout(FloatLayout): pass
creating the App class in which name
#.kv file is to be named Float_Layout.kv class Float_LayoutApp(App): # defining build() def build(self): # returning the instance of root class return FloatLayout()
run the app
if name == "main": Float_LayoutApp().run()
`
This file includes the dynamic placements of button i.e as the screen size changes the button adjust themselves relatively this is the benefit of the FloatLayout..kv file implementation of approach -
Python3 `
#.kv file implementation of FloatLayout
creating button feature
: font_size: 40
# creating button
# a button 30 % of the width and 50 %
# of the height of the layout
size_hint: 0.3, 0.3:
Button:
text: "Helooo !!!!! "
background_color: 0.1, 0.5, 0.6, 1
# positioned at 0 % right and 100 % up / top
# from bottom left, i.e x, top = 0, 100 from bottom left:
pos_hint: {"x":0, "top":1}
Button:
text:"Rajnish:)"
background_color: 1, 0, 0, 1
pos_hint: {"x":0.35, "y":0.3}
Button:
text:"Ravi:)"
background_color: 0.4, 0.5, 0.6, 1
pos_hint: {"x":.7, "bottom":0}
Button:
text:"Sanjeev:)"
background_color: 0, 0, 1, 1
pos_hint: {"x":.7, "top":1}
Button:
text:"Suraj:)"
background_color: 0.8, 0.9, 0.2, 1
pos_hint: {"x":0, "bottom":1}`