KVLang Series – 7 – Dynamic Classes

KVLang Series – 7

← Previous                      Next →

Dynamic Classes

– .kv file
– .py file
– screenshot of output


Dynamic Classes, as opposed to Class Rules, allow you to inherit from a Base Class and allow you to change default values and to create bindings for all its instances, without adding any new Python code.
Dynamic Classes also replace the deprecated Templates concept.
Dynamic Classes
Re-using styles in multiple widgets
Designing with the Kivy Language
Kivy Language

Here is the kvlang file.


# see https://groups.google.com/d/topic/kivy-users/FdOhizCm7Ns/discussion
# KV Lang http://kivy.org/docs/guide/lang.html#
# Accessing Widgets defined in Python http://kivy.org/docs/guide/lang.html#accessing-widgets-defined-inside-kv-lang-in-your-python-code
# Dynamic Classes - http://kivy.org/docs/api-kivy.lang.html
# Referencing Widgets http://kivy.org/docs/guide/lang.html#referencing-widgets

    l_text: '0default'
    t_text: '1default'
    b_text: '2default'
    orientation: 'horizontal'
        text: root.l_text
        text: root.t_text
        text: root.b_text

    orientation: 'vertical'

        l_text: 'DynClassInput1:'
        t_text: 'DynClassDefault Text1'
        b_text: 'DynClassPressMe 1'

        l_text: 'DynClassInput2:'
        t_text: 'DynClassDefault Text2'
        b_text: 'DynClassPressMe 2'

        l_text: 'DynClassInput3:'
        t_text: 'DynClassDefault Text3'
        b_text: 'DynClassPressMe 3'



Because the .kv file uses Dynamic Classes as opposed to Class Rules, the Python File now reverts to its former simpler self.

I.E. In the Python file, it is NOT NECESSARY to define:

from kivy.uix.boxlayout import BoxLayout
from kivy.properties import StringProperty   

class LblTxtBtn(BoxLayout):

    l_text = StringProperty('')
    t_text = StringProperty('')
    b_text = StringProperty('')

class MyLayout(BoxLayout): pass

since it LblTxtBtn & MyLayout are Dynamic Classes in the .kv file.

Just revert to the SIMPLE 0005_nestedLayouts.py, update Window width to 430 & update the .kv filename.

''' 0007_dynamicClasses01.py
**SIMPLE** Python to display Dynamic Classes
Revert to 0005_nestedLayouts.py & update Window width & .kv filename.

import kivy

from kivy.app import App
from kivy.lang import Builder
from kivy.core.window import Window
Window.size = (430,90)

class MyApp(App):

    def build(self):
        self.root = Builder.load_file('0007_dynamicClasses01.kv')
        return self.root

if __name__ == '__main__':

0007_dynamicClasses01 ScreenShot

Here is what this looks like run on Windows XP. In Pixels, it has:

Alt 0007_dynamicClasses01.png


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s