In this video I’ll explain the Box Layout for Kivy and Python.

Kivy comes with several layout options to choose from. In this video we’ll look at Box Layout, which is basically just a stacked layout that’s oriented horizontally or vertically.

Or to put it another way, Box Layout arranges children in a vertical or horizontal box.

We’ll also look at sizing things in Box Layout, and positioning things as well.

Python Code: box.py
(Github Code)

from kivy.app import App
from kivy.uix.widget import Widget
from kivy.properties import ObjectProperty
from kivy.lang import Builder

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

class MyLayout(Widget):
	pass
	

class AwesomeApp(App):
	def build(self):
		return MyLayout()

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

Kivy Design Code: box.kv
(Github Code)

<MyLayout>
	BoxLayout:
		orientation: "vertical"
		size: root.width, root.height

		padding: 50
		spacing: 20

		Button:
			text: "Hello World!"
		
		Button:
			text: "Goodbye World!"

		Button:
			text: "I'm Hungry!"
			pos_hint: {'center_x': 0.5}
			size_hint: (1,.3)
			#width: 200
			#height: 50

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