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

KVLang Series – 8

← Previous                     

Inter-Widget Communication (IWC) with Dynamic Classes

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

Make topic comments here.
See:
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


<LblTxtBtn@BoxLayout>:
    l_text: '0default'
    t_text: '1default'
    b_text: '2default'

    o_text: 'default'
    the_btn: btn

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

<LblTxt@BoxLayout>:
    l_text: '0default'
    t_text: '1default'
    orientation: 'horizontal'
    Label:
        text: root.l_text
    TextInput:
        multiline: False
        text: root.t_text


<MyLayout@BoxLayout>:  
    orientation: 'vertical'
    str_prop0: ltb0.o_text
    str_prop1: ltb1.o_text

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

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

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


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


MyLayout 

.py file

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

''' iwc.py
'''
import kivy
kivy.require('1.8.0')

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__':
    MyApp().run()

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

Advertisements