As when we write the application in Python kivy, to write all the things on the same code make a mess in the code and it is hard to understand that by someone another. Also writing a large code makes hard to maintain the construction of the widget tree and explicit the declaration of bindings.
The KV language, allows us to create own widget tree in a declarative way and to bind the widget properties to each other or to callbacks in a natural way.
How to load kv file:
There are 2-ways to load the .kv
file into code or Application
- By name convention method-
While writing code we will make the App class. For this method, the name of the file and the app class is same and save the kv file with
appclassname.kv
.
Kivy looks for a Kv file with the same name as your App class in lowercase, minus “App” if it ends with ‘App’ e.g:classnameApp ---> classname.kv
If this file defines a Root Widget it will be attached to the App’s root attribute and used as the base of the application widget tree.
The sample code on how to use .kv file in kivy is given below:
# 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
# this restrict the kivy version i.e
# below this kivy version you cannot
# use the app or software
# not coumpulsary to write it
kivy.require(
'1.9.1'
)
# define the App class
# and just pass rest write on kvfile
# not necessary to pass
# can also define function in it
class
kvfileApp(App):
pass
kv
=
kvfileApp()
kv.run()
chevron_rightfilter_none.kv file code save with the same name as the app class –
Label:
text:
(
'[b]Hello[/b] [color = ff0099]World[/color]\n'
'[color = ff0099]Hello[/color] [b]World[/b]\n'
'[b]Hello[/b] [color = ff0099]World:):)[/color]'
)
markup:
True
font_size:
'64pt'
chevron_rightfilter_noneOutput:
- Builder method-
For this method to use you first have to import Builder by writingfrom kivy.lang import builder
Now by the builder you can directly load the whole file as a string or as a file. By doing this for loading .kv file as a file:
Builder.load_file('.kv/file/path')
or, for loading, kv file as a string:
Builder.load_string(kv_string)
# 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 coumpulsary to write it
kivy.require(
'1.9.1'
)
# building kv file as string
kvfile
=
Builder.load_string(
"""
Label:
text:
('[b]Hello[/b] [color = ff0099]World[/color]\\n'
'[color = ff0099]Hello[/color] [b]World[/b]\\n'
'[b]Hello[/b] [color = ff0099]World:):)[/color]')
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()
chevron_rightfilter_noneOutput:
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.