'Pyglet flickers, alternates between two frames

I am trying to subclass pyglet.window.Window to visualize an increasing number of randomly placed crosses. Unfortunately, Pyglet seems to store those crosses, not in one, but in two different, alternating frames. See the following GIF after about 50 iterations.

switching between two frames

Below is the code I used.

import random
import pyglet


class Canvas(pyglet.window.Window):
  def __init__(self, *args, **kwargs):
    super().__init__(*args, **kwargs)
    self.x = 0.
    self.y = 0.

    pyglet.clock.schedule_interval(self.update, .5)

  def update(self, dt: float) -> None:
    if self.has_exit:
      pyglet.app.exit()
    
    self.x = random.random() * self.width
    self.y = random.random() * self.height

  def on_draw(self) -> None:
    pyglet.graphics.draw(2, pyglet.gl.GL_LINES, ('v2f', (self.x - 10, self.y + 10., self.x + 10., self.y - 10.)))
    pyglet.graphics.draw(2, pyglet.gl.GL_LINES, ('v2f', (self.x - 10, self.y - 10., self.x + 10., self.y + 10.)))


def main() -> None:
  canvas = Canvas(width=1024, height=768)
  pyglet.app.run()


if __name__ == '__main__':
  main()

Could someone explain to me what's going on and maybe even provide a solution?

Edit: I don't want to clear the screen between frames. I want to draw on top of the last frame to increase the number of crosses with every call of self.update.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source