Skip to content
Home » Kivy Part 34 – Referencing Objects Using Kivy Properties

Kivy Part 34 – Referencing Objects Using Kivy Properties

Spread the love

In the previous part we were talking about Kivy properties. In this article we’ll see how to use Kivy properties to reference widgets and properties set on other widgets.

But before we delve into the topic, here’s some info for you.

*****

Book Info

I just published my Kivy book, GUI Programming with Python and Kivy. It’s pretty long (over 800 pages) and comprehensive. And, which also counts, easy to read. The book contains lots of illustrations.

This book covers all the basics that you need to know to start programming GUI applications with Python and Kivy. Throughout the book we are building a GUI application from scratch, a fully functional game using all kinds of tools that Kivy has to offer. It’s our Slugrace project, but covered in a much more in-depth manner.

Each part of the book starts with a theoretical introduction of a topic or idea that we then implement in the project. I assume you have no prior knowledge of the Kivy library, but you should have at least some basic knowledge of the Python programming language, including the object-oriented programming paradigm as this is what we will be using a lot in this book.

Kivy book

The book covers all the basic elements of Kivy that you have to know, like widgets, layouts, Kivy ids and properties, graphics, screens, animation, sound. Finally we’ll deploy the app to Windows. It is pretty comprehensive and after you finish it, I’m sure you’ll be able to create your own awesome GUI apps of any kind, not just games.

I hope you will have at least as much fun reading the book as I had writing it.

As far as this Kivy series is concerned, the following parts will contain the most important elements covered in the book. However, some elements will be presented in a simplified way here on my blog or omitted completely.

____________

If you are interested, you can purchase the book in four versions. Here are the links:

1) ebook – pdf version on my website – in full color

Here you can see the description of the book, sample graphics from the book and the full table of contents.

2) ebook – Kindle version on Amazon – in full color

3) paperback version on Amazon – in black and white

4) paperback version on Amazon – in full color

*****

And Now Let’s Move On…

We’re going to be working on our test files again. Here’s the slightly modified kv file:

 # File name: test.kv
  
 <TestLayout>:     
     Button:
         id: _button1
         text: _label.text
         on_press: root.set_text()
  
     Label:
         id: _label
         text: 'hey'
  
     Button:
         id: _button2
         text: str(_button1.height) 

Your Panda3D Magazine

Make Awesome Games and Other 3D Apps

with Panda3D and Blender using Python.

Cool stuff, easy to follow articles.

Get the magazine here (PDF).

Actually, the only differences are that I added the id for the second button and changed its text to just the height of the first button. And here’s the modified Python file:

 # File name: test.py
   
 import kivy
 from kivy.app import App
 from kivy.uix.boxlayout import BoxLayout
  
 class TestLayout(BoxLayout):
     def set_text(self):
         self.ids._label.text = 'CHANGED'
         self.ids._button1.text += '+'
         self.ids._button2.font_size += 5
  
 class TestApp(App):
     def build(self):
         return TestLayout()
  
 if __name__ == '__main__':
     TestApp().run() 

Python Jumpstart Course

Learn the basics of Python, including OOP.

with lots of exercises, easy to follow

The course is available on Udemy.

The set_text method will be called whenever you press the first button. In this example the widgets are referenced from within Python code by means of the ids property. We’re referencing the three widgets by their ids and change some of their properties in code. If you run the program and press the first button several times, you will see something like this:

ids

The ObjectProperty

Instead of using the ids property, we could have used our own properties. Let’s now rewrite the two files to use Kivy properties.

Let’s start with the kv file. Here’s the code with comments:

 # File name: test.kv
  
 <TestLayout>:   
     # Let's define three properties that will reference the three 
     # widgets that we referenced by ids before. You can name them
     # whatever you like.
  
     # You use a widget's id to set the property. For example, to
     # associate the first property with the label, you use the 
     # label's id:
     label: _label
  
     # And here are the other two properties.
     button1: _button1
     button2: _button2
  
     Button:
         id: _button1
         text: _label.text
         on_press: root.set_text()
  
     Label:
         id: _label
         text: 'hey'
  
     Button:
         id: _button2
         text: str(_button1.height)
  

Blender Jumpstart Course

Learn the basics of 3D modeling in Blender.

step-by-step, easy to follow, visually rich

The course is available on Udemy and on Skillshare.

And here’s the Python file:

 # File name: test.py
  
 import kivy
 from kivy.app import App
 from kivy.uix.boxlayout import BoxLayout
  
 # You have to import the properties that you want to use.
 from kivy.properties import ObjectProperty
  
 class TestLayout(BoxLayout):
     # We define the properties we need directly in the class. 
     # In our case the properties are supposed to reference widgets, 
     # which are objects, generally speaking. If you want to reference 
     # a widget, you should use an ObjectProperty. The default value
     # of an ObjectProperty is None, and this will do.
     label = ObjectProperty()
     button1 = ObjectProperty()
     button2 = ObjectProperty()
  
     def set_text(self):
         # Now you can use the properties instead of the ids 
         # property. They must be used on an instance, so you
         # need to add self. 
         self.label.text = 'CHANGED'
         self.button1.text += '+'
         self.button2.font_size += 5
  
 class TestApp(App):
     def build(self):
         return TestLayout()
  
 if __name__ == '__main__':
     TestApp().run() 

If you now run the app, it will work just like before.

Now, in case of the ObjectProperty class, it’s enough to define the property in the kv file and the property will be understood as an ObjectProperty. This means that even if you don’t declare the properties explicitly in the Python file, they will be still available.


Spread the love

Leave a Reply