Table of contents
1.
Introduction
2.
Loading kv file 
2.1.
Name Convention Method
2.1.1.
Python Code
2.1.2.
.kv Program
2.1.3.
Output
2.2.
Builder Method
2.2.1.
Program
2.2.2.
Output
3.
Frequently Asked Questions
3.1.
Do Kivy have a designer?
3.2.
What is the box layout in kivy?
3.3.
What is a widget in kivy?
3.4.
What is float layout in kivy?
4.
Conclusion
Last Updated: Mar 27, 2024

.kv File in Kivy

Author Anjali
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

In this article, we will be discussing a crucial concept related to the .kv file in Kivy. We will be discussing kivy and then we will see different methods to load the .kv file like the name convention method and builder’s method.

Kivy is a GUI tool in python which is platform-independent. It can run on Android, IOS, Linux, Windows, etc. It is mainly used to develop the Android application, but it does not mean that it can not be used on Desktop applications.
When we write applications in Python kivy, to write all things on the same code makes a mess in code and it is difficult to understand someone another. And, also writing a large code makes it tough to maintain the construction of the widget tree and explicit the declaration of bindings.

KV language allows one to create own widget tree in a declarative way and also to bind widget properties to each other or to the callbacks in a natural way.

Loading kv file 

There are two ways to load the .kv file into code or an Application.

Name Convention Method

While writing the code we will make an App class. For the Name Convention method, the name of the file and app class is the same and saves the kv file with appclassname.kv
The Kivy looks for a Kv file with the same name as the App class in the lowercase, minus “App” if it ends with ‘App’.
Example:    
classnameApp ---> classname.kv

If the file defines a Root Widget it will be attached to App’s root attribute and used as the base of the application widget tree.

The sample code to use .kv file in kivy is shown below: 

Python Code

# code to use .kv file in kivy
 
# importing the kivy module
import kivy
 
# base Class of the App inherits from App class.
# app: it always refers to the instance of your application
from kivy.app import App
 
# this restricts the kivy version
# below this kivy version, we cannot
# use the app or the software
kivy.require("1.9.1")
 
# define the App class
# just pass rest write on kvfile
# it is not necessary to pass
# can also define the function in it
class kvfileSampleApp(App):
    pass
 
kv = kvfileSampleApp()
kv.run()

.kv Program

Note: The .kv file code should be saved with the same name as of the app class in the working directory.

Label:
    text:
        ('[b]NINJA[/b] CODERS\\n'
        'CODING [b]NINJAS[/b]\\n'
        '[b]NINJA[/b] CODERS')
    markup: True
    font_size: '64pt'

Output

Builder Method

To use this method first we have to import Builder by writing.

from kivy.lang import builder
You can also try this code with Online Python Compiler
Run Code

Now by the builder, we can directly load the entire file as a string or as a file.

For loading the .kv file as a file: 

Builder.load_file('.kv/file/path')
You can also try this code with Online Python Compiler
Run Code

For loading the .kv file as a string: 

Builder.load_string(kv_string)
You can also try this code with Online Python Compiler
Run Code

Program

# code to use the .kv file as a string in the main file
# code how to use .kv file in kivy

# import kivy module
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

# it is to import Builder
from kivy.lang import Builder

# this restrict the kivy version i.e
# below this kivy version you cannot use the app or software
# not compulsory to write it
kivy.require('1.9.1')

# building kv file as string
kvfile = Builder.load_string("""
Label:
    text:
        ('[b]NINJA[/b] CODERS\\n'
        'CODING [b]NINJAS[/b]\\n'
        '[b]NINJA[/b] CODERS')
    markup: True
    font_size: '64pt'
""")

# define the App class
# and just pass rest write on kvfile
# not necessary to pass
# can also define function in it
class kvfileApp(App):
    def build(self):
        return kvfile

kv = kvfileApp()
kv.run()
You can also try this code with Online Python Compiler
Run Code

Output

Frequently Asked Questions

Do Kivy have a designer?

Kivy Designer is Kivy's tool for designing the graphical user interfaces (GUIs) from Kivy Widgets.

What is the box layout in kivy?

The BoxLayout arranges the children in a vertical or horizontal box. For example: layout = BoxLayout(orientation='vertical').

What is a widget in kivy?

A Widget is a base building block of the GUI interfaces in Kivy. It provides a Canvas that can be used to draw on the screen.

What is float layout in kivy?

The FloatLayout honors pos_hint and size_hint properties of its children. For example: layout = FloatLayout(size=(300, 300)).

Conclusion

In this blog, we started with the kivy framework. Then we discussed the loading of the .kv file in kivy with the help of different methods like the name convention method and the builder’s method. We have discussed both the methods with the help of a sample program code and output. 
 

We hope that this blog helped you enhance your knowledge to improve your understanding of the .kv file in kivy. You may also want to learn more, so please visit interesting articles about kivy, and python libraries.
 

Learning never stops, and to learn more and become more skilled, head over to our practice platform Coding Ninjas Studio, practice top problems, attempt Mock Tests, read informative blogs, and interview experiences. Do upvote our blog to help other ninjas grow. 

 

Happy Learning!

Live masterclass