Python | How to use Multiple kv files in kivy

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.



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:

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


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:

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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] 

chevron_right


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 –

filter_none

edit
close

play_arrow

link
brightness_4
code

# Creating 1st .kv file  
  
<Box1@BoxLayout>:
    Button:
        text: 'B1a'
    Button:
        text: 'B1b'

chevron_right


box2.kv file –

filter_none

edit
close

play_arrow

link
brightness_4
code

# Creating 2nd .kv file
  
<Box2@BoxLayout>:
    Button:
        text: 'B2a'
    Button:
        text: 'B2b'

chevron_right


box3.kv file –

filter_none

edit
close

play_arrow

link
brightness_4
code

# Creating 3rd .kv file 
  
<Box3@BoxLayout>:
    Button:
        text: 'B3a'
    Button:
        text: 'B3b'

chevron_right


Output :



My Personal Notes arrow_drop_up

Competative Programmer

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.