Juanvulcano
Juanvulcano

Reputation: 1396

Text Labels in kivy are not being updated

I am trying to display the numbers of attempts left from a player in a Kivy game. However, while the player can actually run out of attempts in the game, the attempts left are not being updated in the UI. I suspect that it is because the Label just display them once and need to be updated after or maybe it have something to do with Kivy ids.

A simplified version of the code is here

On the main.py we have:

class TreasureHuntGrid(GridLayout):
    attempts = 8
    board = [[0,0][0,0]]
    def __init__(self, *args, **kwargs):
        super(TreasureHuntGrid, self).__init__(*args, **kwargs)

    def lowerattempts(self, button):
        if condition:
            self.attempts = self.attempts - 1

On the .kv file we have:

AnchorLayout:
  anchor_y: 'bottom'
  anchor_x: 'left'
  TreasureHuntGrid:
     id: board
     size: min(self.parent.size), min(self.parent.size)
     size_hint: None, None
  Label:
     size_hint: (1.75, 1)
     height: sp(40)
     text:'You have {} attempts left'.format(board.attempts)

Upvotes: 0

Views: 62

Answers (1)

inclement
inclement

Reputation: 29488

Make your attempts variable a kivy property, then the kv language will automatically bind to update when it changes:

from kivy.properties import NumericProperty

class TreasureHuntGrid(GridLayout):
    attempts = NumericProperty(0)
    ...

Upvotes: 1

Related Questions