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

Referencing widgets

Sometimes, it is necessary to access or reference other widgets in a specific widget tree. In the Kv language, there is a way to do it using IDs.

Getting ready

This recipe will use two common widgets just for reference. The Button and TextInput fields are very common widgets.

How to do it…

This recipe is as follows:

  1. Make a rule
  2. Establish the ID
  3. Call the ID:
    <MyWidget>:
        Button: 
            id: f_but 
        TextInput: 
            text: f_but.state 

How it works…

Let's see the code; this is the first line:

<MyWidget>:

This is the name of the widget we will use, which is a clickable text input. The second line is:

Button:

This defines the button. The third line is:

id: f_but

This gives the button an ID of f_but, which we will use to reference the button. The fourth line is:

TextInput:

This defines the text input. The fifth line is:

text: f_but.state

This is the definition of the text that is in the text input where we are referencing the state of the button. It says that if you do not click the button, the text in the text input is normal, and if you click the button, the text in the text input is shown.

There's more…

An ID is limited in scope to the rule it is declared in, so in the preceding code, f_but cannot be accessed outside the <MyWidget> rule; that is, if we have a second <MyWidget2>, we are not able to reference f_but in <MyWidget2>.

Also ID is a weakref module for the widget and not the widget itself. As a consequence, storing the ID is not sufficient to keep the widget from being garbage collected. To demonstrate:

<MyWidget>:
    label_widget: label_widget.__self__ 
    Button: 
        text: 'Add Button' 
        on_press: root.add_widget(label_widget) 
    Button: 
        text: 'Remove Button' 
        on_press: root.remove_widget(label_widget) 
    Label: 
        id: label_widget 
        text: 'widget'

If we do not use ID.__self__ or in this case label_widget.__self__ just label_widget, we are going to get an error: ReferenceError: weakly-referenced object no longer exists.

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