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
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')
For loading the .kv file as a string:
Builder.load_string(kv_string)
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()