Python | How to use Multiple kv files in kivy

07 Feb, 2020
Kivy is a platform independent GUI tool in Python. As it can be run on Android, IOS, linux and Windows etc. It is basically used to develop the Android application, but it does not mean that it can not be used on Desktops applications.
In this article, we will see how can we use multiple .kv files in a single Application .
This is the Python program, which uses GridLayout as the root widget. In addition to the main kv file, it loads box1.kv, box2.kv and box3.kv. There are also 2 application variables. These variables are referenced from the main kv file.
 Kivy Tutorial – Learn Kivy with Examples.
Basic Approach:

1) import kivy
2) import kivyApp
3) import Gridlayout
4) import Builder
5) Set minimum version(optional)
6) Create Layout class
7) Create App class
8) Set up multiple .kv file
9) return Layout/widget/Class(according to requirement)
10) Run an instance of the class
main.py file of the implementation:
# Multiple .kv file Python code  import kivy     # base Class of your App inherits from the App class.   # app:always refers to the instance of your application  from kivy.app import App  # The GridLayout arranges children in a matrix.# It takes the available space and divides# it into columns and rows, then adds# widgets to the resulting “cells”.from kivy.uix.gridlayout import GridLayout  # Builder is a global Kivy instance used# in widgets that you can use to load other# kv files in addition to the default ones.from kivy.lang import Builder    # Loading Multiple .kv files Builder.load_file('box1.kv')Builder.load_file('box2.kv')Builder.load_file('box3.kv')    # Creating main kv file classclass main_kv(GridLayout):    pass  # Create App classclass MainApp(App):    def build(self):        self.x = 150        self.y = 400        return main_kv()  # run the Appif __name__=='__main__':    MainApp().run()
The main kv file contains a GridLayout with 3 columns. These 3 Columns contains different AnchorLayouts. These all are defined in the main.kv file.
Now the main.kv file:
# Creating the main .kv files# the difference is that it is# the heart of the Application# Other are just Organs  <main_kv>:      # Assigning Grids    cols: 3      # Creating AnchorLayout    AnchorLayout:        anchor_x: 'left'        anchor_y: 'center'          # Canvas creation        canvas:            Color:                rgb: [1, 0, 0]            Rectangle:                pos: self.pos                size: self.size                  Box1:            size_hint: [None, None]            size: [app.x, app.y]      AnchorLayout:        anchor_x: 'center'        anchor_y: 'center'        canvas:            Color:                rgb: [0, 1, 0]            Rectangle:                pos: self.pos                size: self.size        Box2:            size_hint: [None, None]            size: [app.x, app.y]      AnchorLayout:        anchor_x: 'right'        anchor_y: 'center'        canvas:            Color:                rgb: [0, 0, 1]            Rectangle:                pos: self.pos                size: self.size        Box3:            size_hint: [None, None]            size: [app.x, app.y] 
Now as shown in the Outputs there are different buttons in each grid to create Buttonsin every grid we are using Different .kv files.
box1.kv file –
# Creating 1st .kv file    <Box1@BoxLayout>:    Button:        text: 'B1a'    Button:        text: 'B1b'
box2.kv file –
# Creating 2nd .kv file  <Box2@BoxLayout>:    Button:        text: 'B2a'    Button:        text: 'B2b'
box3.kv file –
# Creating 3rd .kv file   <Box3@BoxLayout>:    Button:        text: 'B3a'    Button:        text: 'B3b'
Output :
Python-gui
Python-kivy
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Python Dictionary
Different ways to create Pandas Dataframe
Enumerate() in Python
Python String | replace()
How to Install PIP on Windows ?
*args and **kwargs in Python
Python Classes and Objects
Python OOPs Concepts
Convert integer to string in Python
Introduction To PYTHON