Kivy - Is there any way to bind Python functions to Widgets created in the kv language?
<p>I am trying to create a simple Pokemon battle simulator. A trainer has 6 PokÃ©mon, stored in a list. I have labels in the .kv file displaying the desired information.<br>
My problem is that if I have the text property of the labels set to a Python variable:</p>

<pre><code>text: '{}/{}'.format(root.pokemon.stats['cHealth'], root.pokemon.stats['Health'])
</code></pre>

<p>then the labels get constantly updated, but when set to a Python function:</p>

<pre><code>text: root.pokemon.getHP()
</code></pre>

<p>with the getHP() function like:</p>

<pre><code>def getHP(self):
    return '{}/{}'.format(self.stats['cHealth'], self.stats['Health'])
</code></pre>

<p>they do not update when the health is changed, but only when the list holding the PokÃ©mon is changed. (eg. the order of the PokÃ©mon is changed) Is there any way to get the binding to work when calling a function, or must all of the function calls be replaced by their return value?</p>

<p>the <em>kv lang</em> will auto-bind properties that are declared in it, so can you send the stats to your function and the binding will occur :)</p>

<pre><code>text: root.pokemon.getHP(root.pokemon.stats)
</code></pre>

<p>Each time that <strong>stats</strong> is changed, the function will be called.</p>

python kivy kivy-language