-
Book Overview & Buying
-
Table Of Contents
-
Feedback & Rating

Kivy Cookbook
By :

This recipe will teach you how to take advantage of reusing styles for different widgets, a procedure that could be useful for the scalability of a system.
We will use this example of a file, e8.kv
, where two widgets are defined:
<MyWidget1>: Button: on_press: self.text(txt_inpt.text) TextInput: id: txt_inpt <MyWidget2>: Button: on_press: self.text(txt_inpt.text) TextInput: id: txt_inpt
We must note that they are very similar, and actually just the name of the widget is different between them.
The following steps provide a way to join the two widgets:
<MyWidget1,MyWidget2>: Button: on_press: self.text(txt_inpt.text) TextInput: id: txt_inpt
In this case, by separating the class names with a comma, all the classes listed in the declaration will have the same KV properties and you could join any number of similar widgets.
In the Python code, the widgets could do different tasks, as in the next portion of code:
class MyWidget1(Widget): def text(self, val): print('text input text is: {txt}'.format(txt=val)) class MyWidget2(Widget): writing = StringProperty('') def text(self, val): self.writing = val
Similarly, you can join the widgets in the Kv language.
If you want to get more details about widgets, see the recipes in Chapter 4, Widgets.
Change the font size
Change margin width
Change background colour