Python | Kivy .kv File

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

  1. 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:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

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

    
    

    .kv file code save with the same name as the app class –

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    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_right

    
    

    Output:

  2. Builder method-
    For this method to use you first have to import Builder by writing

    from 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)
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    # 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_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 :

1


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