In this video I’ll show you how to use the FloatLayout with Kivy to position the things in your app.

Float Layout allows you to position things very precisely on your app. We do that using size_hint (which takes a width and height argument) and pos_hint (which is a dictionary that can take 6 arguments – x, y, top, bottom, left, and right).

With the float layout, you can move things around your app, or float them around your app any way you like!

Python Code: float_layout.py
GitHub Code: float_layout.py

from kivy.app import App
from kivy.uix.widget import Widget
from kivy.properties import ObjectProperty
from kivy.lang import Builder
from kivy.core.window import Window
#from kivy.uix.floatlayout import FloatLayout

# Designate Our .kv design file 
Builder.load_file('float_layout.kv')

class MyLayout(Widget):
	pass

class AwesomeApp(App):
	def build(self):
		Window.clearcolor = (1,1,1,1)
		return MyLayout()

if __name__ == '__main__':
	AwesomeApp().run()



Kivy Design Code: float_layout.kv
GitHub Code: float_layout.kv

<Button>
	font_size: 32
	size_hint: (0.3, 0.3)

<MyLayout>
	
	FloatLayout:
		size: root.width, root.height
	
		
		Button:
			text: "Top Left"
			# {"x", "y", "top", "bottom", "left", "right"}
			pos_hint: {"x":0, "top":1}
		
		Button:
			text: "Top Right"
			pos_hint: {"x":.7, "top":1}
		Button:
			text: "Center"
			pos_hint: {"x":0.35, "top":0.66}

		Button:
			text: "Bottom Left"
			pos_hint: {"x":0}

		Button:
			text: "Bottom Right"
			pos_hint: {"x":.7, "bottom":1}
			
		

John Elder

John is the CEO of Codemy.com where he teaches over 100,000 students how to code! He founded one of the Internet's earliest advertising networks and sold it to a publicly company at the height of the first dot com boom. After that he developed the award-winning Submission-Spider search engine submission software that's been used by over 3 million individuals, businesses, and governments in over 42 countries. He's written several Amazon #1 best selling books on coding, and runs a popular Youtube coding channel.

View all posts

Add comment

Your email address will not be published. Required fields are marked *

John Elder