From 2169c508abb29a89e6287cf3effd77fda26e14b8 Mon Sep 17 00:00:00 2001 From: Matthew Einhorn Date: Sat, 4 May 2013 15:44:19 -0400 Subject: [PATCH] Document resuing styles. --- doc/sources/guide/lang.rst | 52 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) 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 --------------------------------