mirror of https://github.com/kivy/kivy.git
Document resuing styles.
This commit is contained in:
parent
f71637ec9e
commit
2169c508ab
|
@ -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
|
||||
|
||||
<MyFirstWidget>:
|
||||
Button:
|
||||
on_press: self.text(txt_inpt.text)
|
||||
TextInput:
|
||||
id: txt_inpt
|
||||
|
||||
<MySecondWidget>:
|
||||
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
|
||||
--------------------------------
|
||||
|
||||
|
|
Loading…
Reference in New Issue