Python | Kivy .kv File



The KV language allows us to create our own widget tree in a declarative way and associate widget properties with each other or with callbacks in a natural way.

How to load the kv file:

There are 2 ways to load .kv file into code or application

  1. By name

    When we write the code, we will make the App class. For this method, the file name and application class are the 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, except for “App” if it ends in “App”, for example:

     classnameApp --- & gt; classname.kv 

    If this file defines a root widget, it will be attached to the root attribute of the application and used as the base of the application widget tree.

    Sample code on how to use the .kv file in kivy, below:

    # code how to use the .kv file in kiw

     
    # import cool module

    import kivy

     
    # The base class of your application inherits from the class application.
    # app: always refers to your application instance

    from kivy.app import App

    < p>  
    # this limits the kivy version ie
    # below this version you cannot
    # use the application or software
    # optional write this

    kivy.require ( `1.9.1` )

     
    # define the application class
    # and just skip the rest write on the cfile
    # no need to go
    # can also define a function in it

    class kvfileApp (App):

    pass

     

    kv = kvfileApp ()

    kv.run ()

    Save the .kv file code under the same name as the application class —

    Label:

    text:

      ( `[b] Hello [/ b] [color = ff0099] World [/ color]`

    `[color = ff0099] Hello [/ color] [b] World [/ b]`

    `[b] Hello [/ b] [color = ff0099] World:):) [/ color]` )

    markup: True

    font_size: `64pt`

    Exit:

  2. Builder Method
    To use this method, you first need to import the Builder by writing
     from kivy.lang import builder 

    The developer can now directly load the entire file as a string or file. Doing this to load the .kv file as a file:

     Builder.load_file (`.kv / file / path`) 

    or, to load, the kv file as a string:

    Builder.load_string(kv_string)

    # code to use the .kv file as a line in the main file
    # code how to use the .kv file in kiw

     
    # import kivy module

    import kivy

     
    # Your application base class inherits from the application class.
    # app: always refers to your application instance

    from kivy.app import App

     
    # this is the import Builder

    from kivy.lang import Builder

     
    # this limits the kivy version i.e.
    # below this version you cannot use the application or software
    # not be sure to write this

    kivy.require ( `1.9.1` )

     
    # build the kv file as a string

    kvfile = Builder.load_string ( "" "

     Tag:

    text:

    (& # 39; [b] Hello [/ b] [color = ff0099] World [/ color] // n & # 39;

    & # 39; [color = ff0099] Hello [/ color] [b] World [/ b ] // n & # 39;

    & # 39; [b] Hi [/ b ] [color = ff0099] World :) :) [/ color] & # 39;)

    markup: true

      font_size: 64pt

    "" " )

      
    # define the application class
    # and just skip the rest write on the cfile
    # not need to pass
    # can also define a function in it

    class kvfileApp (App):

    def build ( self ):

    return kvfile

     

    kv = kvfileApp ()

    kv.run ()

    Output: