KVLang Series – 8 – Inter-Widget Communication (IWC)

KVLang Series – 8

← Previous                     

Inter-Widget Communication (IWC) with Dynamic Classes

– .kv file
– .py file
– Screenshot of output

Make topic comments here.
Dynamic Classes
Referencing Widgets
Accessing Widgets

.kv file

Here is the kvlang file. It contains 3 Dynamic classes, all derived from BoxLayout.

  • <LblTxtBtn>
  • <LblTxt>
  • <MyLayout>

The MyLayout Class instantiates
– 2 <LblTxt> classes and
– 2 <LblTxtBtn> classes

The MyLayout Class is instantiated as the Root Rule on the last line.

Pressing the ‘myTxtVal to …’ button in a LblTxtBtn class, copies the value of that LblTxtBtn’s TextInput widget into the “corresponding” LblTxt’s TextInput widget.

# iwc.kv
# 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'

    o_text: 'default'
    the_btn: btn

    orientation: 'horizontal'
        text: root.l_text
        id: txt
        multiline: False
        text: root.t_text
        id: btn
        text: root.b_text 
        on_release: root.o_text = txt.text

    l_text: '0default'
    t_text: '1default'
    orientation: 'horizontal'
        text: root.l_text
        multiline: False
        text: root.t_text

    orientation: 'vertical'
    str_prop0: ltb0.o_text
    str_prop1: ltb1.o_text

        id: lt0
        l_text: 'LblTxtInput0:'
        t_text: root.str_prop0

        id: lt1
        l_text: 'LblTxtInput1:'
        t_text: root.str_prop1

        id: ltb0
        l_text: 'DynClassInput0:'
        t_text: 'ltb0Text0'
        b_text: 'myTxtVal to lt0'

        id: ltb1
        l_text: 'DynClassInput1:'
        t_text: 'ltb1Text1'
        b_text: 'myTxtVal to lt1'


.py file

Here is the very simple Python file. All the work is done in the .kv file.

''' iwc.py
import kivy

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

class MyApp(App):

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

if __name__ == '__main__':

Screenshot of output

Here is what this looks like run on Windows XP. The operator has just pressed the ‘myTxtVal to lt0’ button.

Alt interWidgetComm.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