diff --git a/doc/sources/guide/lang.rst b/doc/sources/guide/lang.rst index 7cffed26d..d7e7e2b3e 100644 --- a/doc/sources/guide/lang.rst +++ b/doc/sources/guide/lang.rst @@ -334,6 +334,58 @@ template instead, like so: `ctx` is a keyword inside a template that can be used to access the individual attributes of each instance of this template. +Re-using styles in multiple widgets +------------------------------------------------------------ + +Consider the code below in my.kv: + +.. code-block:: kv + + : + Button: + on_press: self.text(txt_inpt.text) + TextInput: + id: txt_inpt + + : + Button: + on_press: self.text(txt_inpt.text) + TextInput: + id: txt_inpt + +In myapp.py: + +.. code-block:: py + + ... + class MyFirstWidget(BoxLayout): + + def text(self, val): + print 'text input text is: {txt}'.format(txt=val) + + class MySecondWidget(BoxLayout): + + writing = StringProperty('') + + def text(self, val): + self.writing = val + ... + +Because both classes share the same .kv style, this design can be simplified +if we reuse the style for both widgets. You can do this in .kv as follows. +In my.kv: + +.. code-block:: kv + + <-MyFirstWidget,-MySecondWidget>: + Button: + on_press: self.text(txt_inpt.text) + TextInput: + id: txt_inpt + +By appending a dash before each class name, all the classes listed in the +declaration will have the same kv properties. + Designing with the Kivy Language --------------------------------