Reusing styles in multiple widgets

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.

Getting ready

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.

How to do it…

The following steps provide a way to join the two widgets:

  • Let's conserve just one of the widgets.
  • The name of the discarded widget will be added to name of the conserved widget, by separating with a ...

Get Kivy Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.