Skip to content
Advertisement

Kivy – Bind Label Text To Variable (Python Only)

I have been trying to get my labels to auto-update themselves for quite a while now and I have read over a dozen StackOverflow questions but to no avail.

I have an global object that contains an integer value that I want to be displayed with a label inside of one of my widget classes.

The widget class looks like this:

JavaScript

When the value of enemy.health is changed in the program, I want my label to change as well. I do not want to use any of the kivy language because I prefer having just one main python file.

The enemy object is created with an entity class. Here is the entity code:

JavaScript

When I run the code I press a button that calls a function that changes the enemy health and then I get an error:

global name ‘health_label’ is not defined

Somehow, when the update_health function is called, the program doesn’t see the health_label variable that was created in the init.

Advertisement

Answer

You need to use the bind method, something like the following

JavaScript

This is just a basic example off the top of my head, it can be made neater depending on the structure of your program. If enemy.health is a string, you can just do enemy.bind(health=health_label.setter('text')).

For this to work, the health attribute must be a kivy property:

JavaScript

In your code it seems that enemy is a global object. There is quite likely a better way to do that. Also, I recommend using kv language – there is not really any special value in putting your code in one file (indeed, it’s commonly considered bad practice once it gets non-trivial), and kv makes a lot of things easier where python just isn’t suited to certain tasks.

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement