Skip to content Skip to sidebar Skip to footer

Changing A Kivy Widget Text

I am attempting to create a pong game using kivy. However, I am running into issues when I try to end a game after someone scores 10 points. Everything is working well except tha

Solution 1:

You would use the same method to change the game over label that you use to change the score labels when the score changes.

If you add an ObjectProperty to PongGame in main.py:

end_label = ObjectProperty()

And in your kv file under PongGame, write:

end_label:game_over_label# linking them up

You could add a function to PongGame class in main.py:

defcheck_score(self):
    scores = [self.player1.score,
              self.player2.score]
    for s inscores:if s == 10:
            self.end_label = "Game Over"return

Then in your update function, you could call this function any time the scores are incremented, for instance:

self.player1.score += 1self.check_score()

and the same for self.player2.score

Solution 2:

one way to find the ID inside the tree is to put a "redirector" in the root of the widget

In .kv, right under PongGame:

        gameoverlabel: game_over_label

in .py under the " if count1 or count2 >= 10:" add

self.gameoverlabel.text = "win"

hope this is helpful

Post a Comment for "Changing A Kivy Widget Text"