Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Kivy Cookbook

You're reading from   Kivy Cookbook Enhance your skills in developing multi-touch applications with Kivy

Arrow left icon
Product type Paperback
Published in Aug 2015
Publisher
ISBN-13 9781783987382
Length 246 pages
Edition 1st Edition
Languages
Tools
Arrow right icon
Authors (2):
Arrow left icon
Hugo Solis Hugo Solis
Author Profile Icon Hugo Solis
Hugo Solis
Hugo Solis Hugo Solis
Author Profile Icon Hugo Solis
Hugo Solis
Arrow right icon
View More author details
Toc

Table of Contents (11) Chapters Close

Preface 1. Kivy and the Kv Language FREE CHAPTER 2. Input, Motion, and Touch 3. Events 4. Widgets 5. Graphics – Canvas and Instructions 6. Advanced Graphics – Shaders and Rendering 7. The API in Detail 8. Packaging our Apps for PC 9. Kivy for Mobile Devices Index

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 comma:
    <MyWidget1,MyWidget2>:
        Button: 
            on_press: self.text(txt_inpt.text) 
        TextInput: 
            id: txt_inpt 

How it works…

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.

There's more…

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.

See also

If you want to get more details about widgets, see the recipes in Chapter 4, Widgets.

You have been reading a chapter from
Kivy Cookbook
Published in: Aug 2015
Publisher:
ISBN-13: 9781783987382
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image