remove text

i want to display a new number each time the key “w” is pressed:

def EventKeyW(self):

   text = TextNode('node name')
   textNodePath = aspect2d.attachNewNode(text)

it’s working, but the new numbers are just displayed over the old numbers/you can’t really read them.
-> how can i remove the old text/rerender the background? thx

Each time you call TextNode(), you are creating a new instance of a node, and then you drop the handle to it so there’s no way to remove it later.

Keep the handle to your TextNode around, and then you can remove it when you create a new one. Or, better yet, don’t even bother to create a new one; once you have the handle to the old one, you can just change the text.

Do something like this instead:

def __init__(self):
    self.text = TextNode('node name')
    textNodePath = aspect2d.attachNewNode(self.text)

def EventKeyW(self):