Python | Creating a Box Layout widget using a .kv file



Now in this article, we will learn about using the Box layout widget in kivy using a .kv file and how to add some functionality to it like color, size, etc.

BoxLayout:
Kivy offers several layouts to store widgets in the right places in the application. BoxLayout — it is a simple yet powerful layout, often used either in a nested way or in a simple and straightforward way. BoxLayout arranges widgets either vertically stacked one above the other or horizontally stacked one behind the other. If you do not provide a size hint, the child widgets divide the parent widget`s size by equal or respectively.

Basic Approach to follow while creating button:
1 ) import kivy
2) import kivyApp
3) import BoxLayout
4) set minimum version (optional)
5) Extend the class
6) set up .kv file (name same as the Appclass)
7) Return layout
8) Run an instance of the class

main.py BoxLayout file —

# Your application base class inherits from the application class.
# app: always refers to your application instance

from kivy.app import App 

  
# BoxLayout places children in a vertical or horizontal border.
# or help put kids in the right place.

from kivy.uix.boxlayout import BoxLayout

 
########################################## ####

 
# create the root widget used in the .kv file

class KVBL (BoxLayout):

"" "

  no need to do anything here as

we are building things in the .kv file

"" "

pass

 
######################################## ########
# class in which the name of the .kv file should be named KVBoxLayout.kv.

class KVBoxLayoutApp (App): 

 

def build ( self ):

# returns an instance of the KVBL class

  return KVBL ()

  
##################################### ############

 
# create the object root for the BoxLayoutApp () class

root = KVBoxLayoutApp () 

  
# run function runs the entire program
# i.e. run () method, which calls
# target function is passed to constructor.
root.run () 

KVBoxLayout.kv file main.py file

& lt; KVBL & gt ;:
# you can change it to BoxLayout but have
# change everywhere, including .py

 
######################### ########################## ######

  

# To place widgets side by side

# use horizontal BoxLayo ut.

# To place widgets above / below each other

# use vertical BoxLayout.

  

# orientation: "horizontal"

 

orientation: `vertical`

  
###### ################################################## ###### #

 

# define buttons in block layout format

  # and add color, size, etc. . d.

< p> # you can use accordingly

Button:

text: "Btn1"

background_color: 0 , 1 , 1 , 1

font_size: 40

 

  Button:

text: "Btn2"

background_color: 0 , 1 , 0 , 1

font_size: 20

 

Button:

text: "Btn3"

background_color: 0 , 0 , 1 , 1

font_size: 35

< p>  

Button:

text: "Btn4"

background_color: 1 , 0 , 1 , 1

font_size: 30

 

Button:

text: " Btn5 "

  background_color: 1 < code class = "plain">, 0 , 0 , 1

font_size: 25

Exit:

1) When vertical orientation is set

2) When the orientation is horizontal

Link:
https://kivy.org/doc/stable/api-kivy.uix.boxlayout.html