Introduction
Kivy is an independent GUI tool in Python that can be used to create Android, IOS, Linux, and Windows applications. Kivy provides the functionality to write the code for once and run it on different platforms. Though, most applications built on kivy are Android applications we can use it for desktop applications as well. It is very important to have a layout of any component we are working on. And one of the easiest ways to work on layout is by using a box layout.
Thus, in this blog, we will learn how we can create a box layout using a .kv file.
Box Layout
Box Layout can be used both in a nested way as well as a simple plain way. It arranges the widgets either in a vertical fashion on in a horizontal fashion. When no size hint is provided then the child widgets are divided equally or accordingly.
main.py
#first we import the app and the boxlayout
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
#then we create a dummy class that can be empty as we are making all the elements in the kivy file
class TestBL(BoxLayout):
pass
#then we create the function to be run with the name of the kv file and App appended so that the kv file is run along with the .py file
class testboxlayoutApp(App):
def build(self):
return TestBL()
root = testboxlayoutApp()
root.run
Now we create the testboxlayout.kv file for main.py. The kv file contains three buttons that will be arranged vertically though the default value here is horizontal. Then we create three buttons as child elements that are arranged vertically.
TestBoxLayout.kv
<TestBL>:
#defining orientation of the box
orientation: 'vertical'
#desiging the buttons as a child element of the box
Button:
text: "Hello"
background_color: 1, 1, 1, 1
Button:
text: "World"
background_color: 0, 1, 0, 1
Button:
text: "Again"
background_color: 0, 0, 0, 0
Output





