Open In App

Python | How to use Multiple kv files in kivy

Last Updated : 07 Feb, 2020
Like Article

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 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 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 
# Creating main kv file class
class main_kv(GridLayout):
# Create App class
class MainApp(App):
    def build(self):
        self.x = 150
        self.y = 400
        return main_kv()
# run the App
if __name__=='__main__':

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
    # Assigning Grids
    cols: 3
    # Creating AnchorLayout
        anchor_x: 'left'
        anchor_y: 'center'
        # Canvas creation
                rgb: [1, 0, 0]
                pos: self.pos
                size: self.size
            size_hint: [None, None]
            size: [app.x, app.y]
        anchor_x: 'center'
        anchor_y: 'center'
                rgb: [0, 1, 0]
                pos: self.pos
                size: self.size
            size_hint: [None, None]
            size: [app.x, app.y]
        anchor_x: 'right'
        anchor_y: 'center'
                rgb: [0, 0, 1]
                pos: self.pos
                size: self.size
            size_hint: [None, None]
            size: [app.x, app.y] 

Now as shown in the Outputs there are different buttons in each grid to create Buttons
in every grid we are using Different .kv files.

box1.kv file –

# Creating 1st .kv file  
        text: 'B1a'
        text: 'B1b'

box2.kv file –

# Creating 2nd .kv file
        text: 'B2a'
        text: 'B2b'

box3.kv file –

# Creating 3rd .kv file 
        text: 'B3a'
        text: 'B3b'

Output :

Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads