Search code examples
pythonarcade

How to change the content of text in an Arcade window?


I am currently trying to create a text that can be updated whenever I hit a button. After going through the documentation, I saw the arcade.draw_text function is returning a text_sprite object, but it seems unable to change the text of that object.

Am I doing it completely the wrong way? Or there is a trick I did not get into yet?


Solution

  • Yes, you can do it. Following script changes text on mouse click:

    import arcade
    
    class MyGame(arcade.Window):
        def __init__(self):
            super().__init__(600, 400)
            self.text = 'Waiting for click...'
    
        def on_draw(self):
            arcade.start_render()
            arcade.draw_text(self.text, 300, 200, arcade.color.RED, 30, anchor_x='center')
    
        def on_mouse_release(self, x, y, button, key_modifiers):
            self.text = 'Clicked!'
    
    MyGame()
    arcade.run()
    

    Text before click:

    enter image description here

    Text after click:

    enter image description here