Kivy is a platform independent GUI tool in Python. As it can be run on Android, IOS, linux and Windows etc. Kivy provides you the functionality to write the code for once and run it on different platforms. It is basically used to develop the Android application, but it does not mean that it can not be used on Desktops applications.
Now in this article, we will learn about the use of Box layout widget in kivy using the .kv
file and how to add some features like color, size etc to it.
BoxLayout:
Kivy offers several layouts to keep the widgets at the desired places on an application. BoxLayout is a simple yet powerful layout often used either in a nested way or in a simple plain way. BoxLayout arranges widgets in either in a vertical fashion that is one on top of another or in a horizontal fashion that is one after another. When you will not provide any size-hint then the child widgets divides the size of its parent widget equally or accordingly.
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
file of BoxLayout –
# base Class of your App inherits from the App class. # app:always refers to the instance of your application from kivy.app import App # BoxLayout arranges children in a vertical or horizontal box. # or help to put the childrens at the desired location. from kivy.uix.boxlayout import BoxLayout ############################################### # creating the root widget used in .kv file class KVBL(BoxLayout): ''' no need to do anything here as we are building things in .kv file ''' pass ################################################# # class in which name .kv file must be named KVBoxLayout.kv. class KVBoxLayoutApp(App): def build( self ): # returning the instance of KVBL class return KVBL() ################################################## # creating the object root for BoxLayoutApp() class root = KVBoxLayoutApp() # run function runs the whole program # i.e run() method which calls the # target function passed to the constructor. root.run() |
KVBoxLayout.kv
file of main.py
file
<KVBL>: # you can change it to BoxLayout but have # to change everywhere including .py ######################################################## # To position widgets next to each other, # use a horizontal BoxLayout. # To position widgets above/below each other, # use a vertical BoxLayout. # orientation: 'horizontal' orientation: 'vertical' ######################################################### # defining the buttons in the box layout format # and adding colour, size etc to it. # 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 Button: text: "Btn4" background_color: 1 , 0 , 1 , 1 font_size: 30 Button: text: "Btn5" background_color: 1 , 0 , 0 , 1 font_size: 25 |
Output:
1) When orientation set is Vertical
2) When orientation set is Horizontal
Reference:
https://kivy.org/doc/stable/api-kivy.uix.boxlayout.html