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 ...

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.