python-3.xpygletflicker

What is causing the flicker on an opened window in Pyglet?


GIF displaying issue

When I run the following code, I get the initial window in the GIF, and on a click it opens a new window, however the shapes in the new window are flickering.

I've tried clearing both windows before drawing as can be seen in the code however i'm not sure what is causing the flicker.

import pyglet
from pyglet import shapes
from pyglet.window import mouse

window = pyglet.window.Window(960, 540)
window2 = pyglet.window.Window(960, 540, visible=False)
batch = pyglet.graphics.Batch()
batch2 = pyglet.graphics.Batch()

class window_one():
    
    circle = shapes.Circle(700, 150, 100, color=(50, 225, 30), batch=batch)
    square = shapes.Rectangle(200, 200, 200, 200, color=(55, 55, 255), batch=batch)
    rectangle = shapes.Rectangle(250, 300, 400, 200, color=(255, 22, 20), batch=batch)
    rectangle.opacity = 128
    rectangle.rotation = 33
    line2 = shapes.Line(150, 150, 444, 111, width=4, color=(200, 20, 20), batch=batch)



class window_two():
    line = shapes.Line(100, 100, 100, 200, width=19, batch=batch2)
    star = shapes.Star(800, 400, 60, 40, num_spikes=20, color=(255, 255, 0), batch=batch2)

    
@window.event
def on_draw():
    window.clear()
    window2.clear()
    batch.draw()

@window.event
def on_mouse_press(x,y,button, modifier):
    window2.set_visible(True)
    batch2.draw()


pyglet.app.run()

I tried the above code and was expecting a window to appear on a click and the shapes to display "solid".

I am running through IDLE on a macbook.


Solution

  • Each pyglet.window needs its own on_draw and on_mouse_press event call back:

    @window.event
    def on_draw():
        window.clear()
        batch.draw()
    
    @window2.event
    def on_draw():
        window2.clear()
        batch2.draw()
    
    @window.event
    def on_mouse_press(x, y, button, modifier):
        window.set_visible(False)
        window2.set_visible(True)
    
    @window2.event
    def on_mouse_press(x, y, button, modifier):
        window.set_visible(True)
        window2.set_visible(False)